There's no need for map
there, you just need to loop through the array, not map the array. So all the usual ways of looping through arrays apply, such as forEach
:
const data = [ {id: 111, name: 'AAA'}, ...];
data.forEach(item => {
item.copyOfId = item.id;
});
or for-of
:
const data = [ {id: 111, name: 'AAA'}, ...];
for (const item of data) {
item.copyOfId = item.id;
}
As Mörre points out in a comment, in functional programming map
is indeed what you would reach for, but usually you'd create new objects to populate the new array, instead of modifying (mutating) the objects:
const data = [ {id: 111, name: 'AAA'}, ...];
const result = data.map(item => {
return {...item, copyOfId: item.id};
});
(That example uses spread properties, which are only a Stage 3 proposal but are supported by up-to-date Chrome and Firefox. You could use Object.assign
instead. Either way, it's a shallow copy if that's important.)