0

const get = function() {
  return {
    name: 'me'
  }
}
const array = Array(10).fill(get());
console.log(array);

array.map(item => {
  console.log('1', item);
  item.a = '555'
  console.log('2', item);
  return item;
})

As you see, I modify element in the array like this item.a = '555'. I might console.log will output like this:

1 { name: 'me' } 
2 { name: 'me', a: '555' } 

but, actually I got this:

1 { name: 'me', a: '555' } 
2 { name: 'me', a: '555' } 

How to understand this?

Todd Mark
  • 1,847
  • 13
  • 25

0 Answers0