Array map() adds a field to each object in the array in a loop

When we request the interface, for convenience, want to add a field to each object loop in the array in the returned data? ??
Convenient method:

arr原数组为[
   {fruit: 'apple',price: '25'},
   {fruit: 'banana',price: '10'},
   {fruit: 'pineapple',price: '15'}
]
let arr_ = []
arr.map((item, index) => {
	_arr.push(Object.assign({},item,{flag: false}))
}))
// arr_返回结果
[
   {fruit: 'apple',price: '25',flag: false},
   {fruit: 'banana',price: '10',flag: false},
   {fruit: 'pineapple',price: '15',flag: false}
]

Of course, you can also use the Array forEach() method
arr.forEach((v, i) => { v['flag'] =false })

The result is the same, is
there any way to remember to leave a message!

Guess you like

Origin blog.csdn.net/lqlq54321/article/details/114025665