2 回答

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

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" }],
}
添加回答
舉報(bào)