比较数组并将更改保存在更改数组中

我想比较 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" }

]


慕侠2389804
浏览 101回答 2
2回答

慕尼黑的夜晚无繁华

您的数据有一个小问题,Ghost并且Car具有相同的 ID。修复此问题后,您可以使用以下代码创建结果数组:const products = [{&nbsp; &nbsp; id: "A32S",&nbsp; &nbsp; title: "Car"&nbsp; },&nbsp; {&nbsp; &nbsp; id: "D12E",&nbsp; &nbsp; title: "Rabbit"&nbsp; },&nbsp; {&nbsp; &nbsp; id: "A33S",&nbsp; &nbsp; title: "Ghost"&nbsp; },&nbsp; {&nbsp; &nbsp; id: "34SC",&nbsp; &nbsp; title: "Apple"&nbsp; },];const newProducts = [{&nbsp; &nbsp; id: "A32S",&nbsp; &nbsp; title: "Ferrari"&nbsp; },&nbsp; {&nbsp; &nbsp; id: "D12E",&nbsp; &nbsp; title: "Rabbit"&nbsp; },&nbsp; {&nbsp; &nbsp; id: "A33S",&nbsp; &nbsp; title: "Ghost"&nbsp; }]const changes = [];function hasSameValue(product, otherProduct, key) {&nbsp; return product[key] === otherProduct[key];}function exists(product, productArray) {&nbsp; for (const existingProduct of productArray) {&nbsp; &nbsp; if (hasSameValue(product, existingProduct, "id")) return true;&nbsp; }&nbsp; return false;}function getExistingProduct(id, productArray) {&nbsp; for (const product of productArray) {&nbsp; &nbsp; if (product.id === id) return product;&nbsp; }}for (const product of newProducts) {&nbsp; if (exists(product, products)) {&nbsp; &nbsp; const existingProduct = getExistingProduct(product.id, products);&nbsp; &nbsp; if (!hasSameValue(product, existingProduct, "title")) {&nbsp; &nbsp; &nbsp; changes.push(product);&nbsp; &nbsp; }&nbsp; }}for (const product of products) {&nbsp; if (!exists(product, newProducts)) {&nbsp; &nbsp; changes.push({&nbsp; &nbsp; &nbsp; id: product.id,&nbsp; &nbsp; &nbsp; removed: true&nbsp; &nbsp; });&nbsp; }}console.log(changes);

千巷猫影

如果您想获取所有更改(添加、删除、更新和未更改)以及检查对象的多个键,您也可以尝试类似的操作。时间复杂度为 O(N),空间复杂度为 O(N)。假设“id”是唯一的。function getProductChanges(oldProductsList, newProductsList) {&nbsp; let oldProductsMap = {};&nbsp; let newProductsMap = {};&nbsp; let addedProductsList = [];&nbsp; let updatedProductsList = [];&nbsp; let removedProductsList = [];&nbsp; let unchangedProductsList = [];&nbsp; // considering id is unique&nbsp; // creating map for old products list&nbsp; oldProductsList.map((oldProduct) => {&nbsp; &nbsp; oldProductsMap[oldProduct.id] = oldProduct;&nbsp; });&nbsp; // creating map for new products list and&nbsp; // filtering newly added, updated and unchanged products list&nbsp; newProductsList.map((newProduct) => {&nbsp; &nbsp; newProductsMap[newProduct.id] = newProduct;&nbsp; &nbsp; let oldProduct = oldProductsMap[newProduct.id];&nbsp; &nbsp; if (oldProduct) {&nbsp; &nbsp; &nbsp; // if an existing product is updated&nbsp; &nbsp; &nbsp; if (JSON.stringify(oldProduct) !== JSON.stringify(newProduct)) {&nbsp; &nbsp; &nbsp; &nbsp; updatedProductsList.push(newProduct);&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; unchangedProductsList.push(newProduct);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; // if a new product is added&nbsp; &nbsp; &nbsp; addedProductsList.push(newProduct);&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; });&nbsp; // populating removedProductsList&nbsp; removedProductsList = oldProductsList.filter((oldProduct) => !newProductsMap[oldProduct.id]);&nbsp; return {&nbsp; &nbsp; added: addedProductsList,&nbsp; &nbsp; updated: updatedProductsList,&nbsp; &nbsp; removed: removedProductsList,&nbsp; &nbsp; unchanged: unchangedProductsList,&nbsp; };}输入:let products = [&nbsp; { id: "A32S", title: "Car" },&nbsp; { id: "D12E", title: "Rabbit" },&nbsp; { id: "A321", title: "Ghost" },&nbsp; { id: "A320", title: "Lion" },];let newProducts = [&nbsp; { id: "A32S", title: "Ferrari" },&nbsp; { id: "D12E", title: "Rabbit" },&nbsp; { id: "A321", title: "Ghost" },&nbsp; { id: "A322", title: "Zebra" },];console.log(getProductChanges(products, newProducts));输出:{&nbsp; added: [{ id: "A322", title: "Zebra" }],&nbsp; removed: [{ id: "A320", title: "Lion" }],&nbsp; unchanged: [&nbsp; &nbsp; { id: "D12E", title: "Rabbit" },&nbsp; &nbsp; { id: "A321", title: "Ghost" },&nbsp; ],&nbsp; updated: [{ id: "A32S", title: "Ferrari" }],}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript