I have an array of objects in them. I want to add two new properties to all objects in that array. The objects position in the array and the length of the array.
my code for this is:
objArr.forEach(function(element) {
element.data.basics.all= objArr.length,
element.data.basics.position= objArr.indexOf(element),
});
While in the forEach-loop the properties are added. I can print them while beeing in the loop. But after the loop has finished the properties are gone.
if have tried Add property to an array of objects but both, forEach and .map does not work.
I also tried:
objArr.forEach(function(element) {
element.data.basics.all= objArr.length,
element.data.basics.position= objArr.indexOf(element),
return element
});
and
objArr.map(function(element) {
element.data.basics.position = objArr.indexOf(element)
element.data.basics.all= objArr.length,
return element;
}
)
and i tried arrow function in all cases, so `...objArr.forEach((element)=>{...})