亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

比較數組并將更改保存在更改數組中

比較數組并將更改保存在更改數組中

慕俠2389804 2023-09-28 15:52:47
我想比較 2 個對象數組(產品)并將所有更改保存到數組中changes。我正在尋找帶有循環的答案,以便即使我內部有 20 個鍵products并且newProducts.產品products: [  { id: "A32S", title: "Car" },  { id: "D12E", title: "Rabbit" },  { id: "A32S", title: "Ghost" },]新產品newProducts: [  { id: "A32S", title: "Ferrari" }, // <--- Change title "Car -> Ferrari"  { id: "D12E", title: "Rabbit" },  { id: "A32S", title: "Ghost" }]變化(期望的輸出)changes: [  { id: "A32S", title: "Ferrari" }]
查看完整描述

2 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

您的數據有一個小問題,Ghost并且Car具有相同的 ID。修復此問題后,您可以使用以下代碼創建結果數組:


const products = [{

    id: "A32S",

    title: "Car"

  },

  {

    id: "D12E",

    title: "Rabbit"

  },

  {

    id: "A33S",

    title: "Ghost"

  },

  {

    id: "34SC",

    title: "Apple"

  },

];


const newProducts = [{

    id: "A32S",

    title: "Ferrari"

  },

  {

    id: "D12E",

    title: "Rabbit"

  },

  {

    id: "A33S",

    title: "Ghost"

  }

]


const changes = [];


function hasSameValue(product, otherProduct, key) {

  return product[key] === otherProduct[key];

}


function exists(product, productArray) {

  for (const existingProduct of productArray) {

    if (hasSameValue(product, existingProduct, "id")) return true;

  }

  return false;

}


function getExistingProduct(id, productArray) {

  for (const product of productArray) {

    if (product.id === id) return product;

  }

}


for (const product of newProducts) {

  if (exists(product, products)) {

    const existingProduct = getExistingProduct(product.id, products);

    if (!hasSameValue(product, existingProduct, "title")) {

      changes.push(product);

    }

  }

}


for (const product of products) {

  if (!exists(product, newProducts)) {

    changes.push({

      id: product.id,

      removed: true

    });

  }

}


console.log(changes);


查看完整回答
反對 回復 2023-09-28
?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

如果您想獲取所有更改(添加、刪除、更新和未更改)以及檢查對象的多個鍵,您也可以嘗試類似的操作。


時間復雜度為 O(N),空間復雜度為 O(N)。假設“id”是唯一的。


function getProductChanges(oldProductsList, newProductsList) {

  let oldProductsMap = {};

  let newProductsMap = {};


  let addedProductsList = [];

  let updatedProductsList = [];

  let removedProductsList = [];

  let unchangedProductsList = [];


  // considering id is unique


  // creating map for old products list

  oldProductsList.map((oldProduct) => {

    oldProductsMap[oldProduct.id] = oldProduct;

  });


  // creating map for new products list and

  // filtering newly added, updated and unchanged products list

  newProductsList.map((newProduct) => {

    newProductsMap[newProduct.id] = newProduct;


    let oldProduct = oldProductsMap[newProduct.id];


    if (oldProduct) {

      // if an existing product is updated

      if (JSON.stringify(oldProduct) !== JSON.stringify(newProduct)) {

        updatedProductsList.push(newProduct);

      } else {

        unchangedProductsList.push(newProduct);

      }

    } 

    else {

      // if a new product is added

      addedProductsList.push(newProduct);

    }  

  });


  // populating removedProductsList

  removedProductsList = oldProductsList.filter((oldProduct) => !newProductsMap[oldProduct.id]);


  return {

    added: addedProductsList,

    updated: updatedProductsList,

    removed: removedProductsList,

    unchanged: unchangedProductsList,

  };

}

輸入:


let products = [

  { id: "A32S", title: "Car" },

  { id: "D12E", title: "Rabbit" },

  { id: "A321", title: "Ghost" },

  { id: "A320", title: "Lion" },

];


let newProducts = [

  { id: "A32S", title: "Ferrari" },

  { id: "D12E", title: "Rabbit" },

  { id: "A321", title: "Ghost" },

  { id: "A322", title: "Zebra" },

];


console.log(getProductChanges(products, newProducts));

輸出:


{

  added: [{ id: "A322", title: "Zebra" }],

  removed: [{ id: "A320", title: "Lion" }],

  unchanged: [

    { id: "D12E", title: "Rabbit" },

    { id: "A321", title: "Ghost" },

  ],

  updated: [{ id: "A32S", title: "Ferrari" }],

}


查看完整回答
反對 回復 2023-09-28
  • 2 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號