在理论开发过程中常常会须要对数据进行解决,比方接口返回数组A和已有数据B,须要找到B跟A雷同的对象,并替换B的值。实现如下:

数组A:const A = [    { id: 1, num: 10, attr: 'apple', spec: '红'    },    { id: 2, num: 11, attr: 'banana', spec: '甜'    },    { id: 3, num: 12, attr: 'peach', spec: '香'    },    { id: 4, num: 13, attr: 'grapes', spec: '圆'    },    { id: 5, num: 14, attr: 'pear', spec: '脆' },    { id: 6, num: 15, attr: 'strawberry', spec: '大' },]const B = [    { id: 4, num: 20, attr: 'grapes', spec: '又圆又大'    },    { id: 5, num: 30, attr: 'pear', spec: '又脆又甜' },    { id: 6, num: 40, attr: 'strawberry', spec: '又香又甜' },]
// 更新A中的数据A.forEach((item1, index) => {  const res = B.filter(item2 => {    return item2.id == item1.id // 找到id一样的item  })  res[0] && A.splice(index, 1, res[0]) // 找到一样的item后,进行替换(B的值替换A的值)})