I have an existing array of ids that I'm trying to iterate over to add each as an id:
key to an existing array of objects. I have tried a number of different loops (for, for in, map, forEach), but I keep having the same outcome - it only adds the first id to each object, so id: 'a'
x 6
An example of what I have
const ids = ['a','b','c','d','e','f']
const objArr = [
{
property: "some value",
}
{
property: "some value",
}
{
property: "some value",
}
{
property: "some value",
}
{
property: "some value",
}
{
property: "some value",
}
]
An example of what I want to achieve
const objArr = [
{
property: "some value",
id: 'a'
}
{
property: "some value",
id: 'b'
}
{
property: "some value",
id: 'c'
}
{
property: "some value",
id: 'd'
}
{
property: "some value",
id: 'e'
}
{
property: "some value",
id: 'f'
}
]
Here is an example of a forEach loop with a nested for in loop which I have tried to no avail.
ids.forEach((item) => {
for (const key in objArr) {
objArr[key].id = item
}
})
Can anyone explain what I'm doing wrong and how to achieve what I'm trying to do?
objArr
is overwriting the previous iteration's key.