Reputation: 13206
I want to update a property in each element in my array to be a particular index. However when I try the following:
static reindexComponentsOnMultiplePages(components) {
return components.forEach((el, idx) => (el.componentIndex = idx));
}
I get returned undefined.
My current array is as follows:
components = [
[ {...}, {...}, {...} ],
[ {...}, {...}, {...} ]
]
I was expecting each property in each element in my array to be updated. I call the method as follows:
pageComponents = MyService.reindexComponentsOnMultiplePages(pageComponents);
Upvotes: 0
Views: 3607
Reputation: 3702
Array.forEach()
returns undefined
as described in the documentation.
There's no need to return the array, you are updating the items in your forEach
loop.
var arr = [{name: 'A', componentIndex: null}, {name: 'B', componentIndex: null}];
console.log(arr);
arr.forEach(function(item, index) {
item.componentIndex = index;
});
console.log(arr);
Just avoid returning anything, you don't need to.
Upvotes: 3