This seems so simple in my head but I can't seem to get the correct output. All I'm trying to do is run through a list of data and assign an object property with another object like so
{
Foo: {
firstname: "sally",
lastname: "jenkins"
},
Bar: {
firstname: "john",
lastname: "smith"
}
}
however with this code every element in the object gets assigned the same values.
let formInfo = {}
let spriteAndId = {}
forms.forEach((form) => {
spriteAndId["id"] = parseInt(form.pokemon.url.substr(form.pokemon.url.length - 6), 10);
spriteAndId["url"] = spriteUrlGen(spriteAndId["id"]);
formInfo[form.pokemon.name] = spriteAndId;
})
I apologize if that is hard to understand but all it does is assign a url ("https://example.com") and id (1234) to the empty object spriteAndId. The variable forms is an array with data already in it.
If I log spriteAndId in the loop I get the correct output with brand new values on each iteration, but it seems it just assigns only one of those iterations to every element in the parent object.
I thought maybe I didn't understand forEach well enough so I implemented it using a for loop and got the same outcome. I can't wrap my head around why it would assign the same object to all the values of formInfo.
Hopefully someone can shed some light on what I'm missing here. Thanks in advance!
spriteAndIdto the inside of the loop.