Comparing the two arrays, keep the amount in array2 that is the same as array1 in array2, and the rest of the amount in array2 is 0

const array1 = [{
    
    itemModel: 订单号1, amount:1},{
    
    itemModel: 订单号2, amount:2}] 
const array2= [{
    
    itemModel: 订单号2, amount:3}] 
将array2与array1数组处理成list3 
const list3 = [{
    
    itemModel: 订单号1, amount:0},{
    
    itemModel: 订单号2, amount:3}]

// 处理数据
const changeArray = (array1, array2) => {
    
    
  const list3 = array1.map((item1) => {
    
    
    const item2 = array2.find((item) => item.itemModel === item1.itemModel);
    const amount = item2 ? item2.amount : 0;
    return {
    
     ...item1, amount };
  });
  return list3;
};

Guess you like

Origin blog.csdn.net/LRQQHM/article/details/131245809
Recommended