第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

比較數(shù)組并將更改保存在更改數(shù)組中

比較數(shù)組并將更改保存在更改數(shù)組中

慕俠2389804 2023-09-28 15:52:47
我想比較 2 個(gè)對(duì)象數(shù)組(產(chǎn)品)并將所有更改保存到數(shù)組中changes。我正在尋找?guī)в醒h(huán)的答案,以便即使我內(nèi)部有 20 個(gè)鍵products并且newProducts.產(chǎn)品products: [  { id: "A32S", title: "Car" },  { id: "D12E", title: "Rabbit" },  { id: "A32S", title: "Ghost" },]新產(chǎn)品newProducts: [  { id: "A32S", title: "Ferrari" }, // <--- Change title "Car -> Ferrari"  { id: "D12E", title: "Rabbit" },  { id: "A32S", title: "Ghost" }]變化(期望的輸出)changes: [  { id: "A32S", title: "Ferrari" }]
查看完整描述

2 回答

?
慕尼黑的夜晚無(wú)繁華

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊

您的數(shù)據(jù)有一個(gè)小問(wèn)題,Ghost并且Car具有相同的 ID。修復(fù)此問(wèn)題后,您可以使用以下代碼創(chuàng)建結(jié)果數(shù)組:


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);


查看完整回答
反對(duì) 回復(fù) 2023-09-28
?
千巷貓影

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

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


時(shí)間復(fù)雜度為 O(N),空間復(fù)雜度為 O(N)。假設(shè)“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" }],

}


查看完整回答
反對(duì) 回復(fù) 2023-09-28
  • 2 回答
  • 0 關(guān)注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)