JSFiddle - React, Tailwind, and code Playground

by jacobwsmith

JavaScript

console.clear();

const list = [
	{id: '1', name: 'foo'},
  {id: '2', name: 'bar'}
];
function mutateArray(array){
  return array.map((item, index) => {
    //item['index'] = index; // <--- mutating the array
    return {...item, index}; // <--- new array
  })
}

const result = mutateArray(list);
console.log('list: ', list);
console.log('result: ', result);