var arr =[{"id":1,"name":2},{"id":3,"name":4},{"id":3,"name":2}] How to loop The IDs of arr? After the loop is released, the values of these IDs are modified. How to save them back?
Personally I highly recommend forEachmap These are more functional ways of writing
function arrGenerator(){
// return 原数组;
return [{"id":1,"name":2},{"id":3,"name":4},{"id":3,"name":2}];
}
var arr = arrGenerator();
var res = arr.map(item => item.id * 2); // 这里处理id 把原数组映射成另外个数组
// =>
// [2, 6, 6]
var arr2 = arrGenerator();
var res2 = arr2.map(item => {
return {
id: item.id * 2, // 这里处理 id
name: item.name + '酱❤❤'
}
});
Operations on arrays are the same as set mapping
Especially this one. . . It’s so intoxicating arr.map(item => item.id * 2)
Put it back after changing:
Traverse the array:
var arr =[{"id":1,"name":2},{"id":3,"name":4},{"id":3,"name":2}]
How to loop The IDs of arr?
After the loop is released, the values of these IDs are modified. How to save them back?
Personally I highly recommend
forEach
map
These are more functional ways of writingOperations on arrays are the same as set mapping
Especially this one. . . It’s so intoxicating
arr.map(item => item.id * 2)
The easy-to-understand method is to directly loop and then make a judgment based on the id you want to change and then reassign it