将对象数组与嵌套对象数组进行比较,得到不匹配的对象数组
Compare array of objects with nested array of objects and get unmatched array of objects
const A = [
{
campaignId: "3964126695264",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
{
campaignId: "137244687845173",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
{
campaignId: "182862063246935",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
{
campaignId: "262747542546680",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
];
const B = {
ProductMetadataList: [
{
asin: "B08FHG17ZH",
sku: "130511"
},
{
asin: "B08FHF74TB",
sku: "130512"
},
{
asin: "B08FHBWP89",
sku: "130513"
},
{
asin: "B08FHFL6GD",
sku: "130514"
},
{
asin: "B08FHDWTK2",
sku: "130515"
},
{
asin: "B08FHFMW1V",
sku: "130516"
},
{
asin: "B08FHF6V8B",
sku: "130517"
},
{
asin: "B08FHFWDVM",
sku: "130518"
},
{
asin: "B08FHFD18R",
sku: "130519"
},
{
asin: "B08JKK4217",
sku: "130520"
},
{
asin: "B08JKP82PB",
sku: "130521"
},
{
asin: "B08JKP82PB",
sku: "13052221"
},
],
};
let difference = B.ProductMetadataList.filter((page1) => {
const result = A.forEach((element) => {
element.portfolioData.portfolioData.find((page2) => {
if (page1.asin === page2.asin) {
page2.sku.includes(page1.sku);
}
});
});
return !result;
});
console.log("difference check ", difference);
无法实现上述结果,因为 page1.asin === page2.asin
不匹配,您可以 console.log(page1.asin === page2.asin)
并理解差异,因为我无法解释。
我正在努力实现:-
将 B 数组与 A 数组进行比较并从数组 B 中获取不匹配的对象。
面临的困难:-
无法检查值是否存在于数组 A - 来自数组 B 的 sku 数组 - sku 值。
只有 2 行
将A数组设为一级数组-这样会更容易找到匹配项
A.map(i => i.portfolioData.portfolioData).flat();
过滤找到的元素
B.ProductMetadataList .filter(i => !assinListA.find(j => i.asin === j.asin && j.sku.includes(i.sku)))
const A = [
{
campaignId: "3964126695264",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
{
campaignId: "137244687845173",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
{
campaignId: "182862063246935",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
{
campaignId: "262747542546680",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
];
const B = {
ProductMetadataList: [
{
asin: "B08FHG17ZH",
sku: "130511"
},
{
asin: "B08FHF74TB",
sku: "130512"
},
{
asin: "B08FHBWP89",
sku: "130513"
},
{
asin: "B08FHFL6GD",
sku: "130514"
},
{
asin: "B08FHDWTK2",
sku: "130515"
},
{
asin: "B08FHFMW1V",
sku: "130516"
},
{
asin: "B08FHF6V8B",
sku: "130517"
},
{
asin: "B08FHFWDVM",
sku: "130518"
},
{
asin: "B08FHFD18R",
sku: "130519"
},
{
asin: "B08JKK4217",
sku: "130520"
},
{
asin: "B08JKP82PB",
sku: "130521"
},
{
asin: "B08JKP82PB",
sku: "13052221"
},
],
};
const assinListA = A.map(i => i.portfolioData.portfolioData).flat();
const res = B.ProductMetadataList
.filter(i => !assinListA.find(j => i.asin === j.asin && j.sku.includes(i.sku)))
console.log(res)
一种简单易读的方法是注册匹配数据 array B
的索引,然后根据这些索引进行过滤。
注意:我减少了数组以使代码更易于阅读。它将与您的数据一起使用。
const A = [
{
campaignId: "3964126695264",
portfolioData: {
portfolioData: [
{ asin: "B08JKK4217", sku: ["130520"]},
{ asin: "B08JKP82PB", sku: ["130521"]}
]
}
},
{
campaignId: "137244687845173",
portfolioData: {
portfolioData: [
{ asin: "B08JKK4217", sku: ["130520"]},
{ asin: "B08JKP82PB", sku: ["130521"]}
]
}
}
]
const B = {
ProductMetadataList: [
{ asin: "B08JKK4217", sku: "130520"},
{ asin: "B08JKP82PB", sku: "130521"},
{ asin: "B08JKP82PB", sku: "13052221"}
]
}
let indexes = [] // matching indexes of array B
for(item of A){
let portfolioData = item['portfolioData']['portfolioData']
portfolioData.forEach((el_A) => {
B.ProductMetadataList.forEach((el_B, i) => {
// conditions
let C1 = el_A.asin === el_B.asin
let C2 = el_A['sku'].join() === el_B['sku']
let C3 = !indexes.includes(i)
// save index if there is a match and the index has not been saved before
if(C1 && C2 && C3) indexes.push(i)
})
})
}
let result = B.ProductMetadataList.filter((el,index) => !indexes.includes(index))
console.log(result)
const A = [
{
campaignId: "3964126695264",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
{
campaignId: "137244687845173",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
{
campaignId: "182862063246935",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
{
campaignId: "262747542546680",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
];
const B = {
ProductMetadataList: [
{
asin: "B08FHG17ZH",
sku: "130511"
},
{
asin: "B08FHF74TB",
sku: "130512"
},
{
asin: "B08FHBWP89",
sku: "130513"
},
{
asin: "B08FHFL6GD",
sku: "130514"
},
{
asin: "B08FHDWTK2",
sku: "130515"
},
{
asin: "B08FHFMW1V",
sku: "130516"
},
{
asin: "B08FHF6V8B",
sku: "130517"
},
{
asin: "B08FHFWDVM",
sku: "130518"
},
{
asin: "B08FHFD18R",
sku: "130519"
},
{
asin: "B08JKK4217",
sku: "130520"
},
{
asin: "B08JKP82PB",
sku: "130521"
},
{
asin: "B08JKP82PB",
sku: "13052221"
},
],
};
let difference = B.ProductMetadataList.filter((page1) => {
const result = A.forEach((element) => {
element.portfolioData.portfolioData.find((page2) => {
if (page1.asin === page2.asin) {
page2.sku.includes(page1.sku);
}
});
});
return !result;
});
console.log("difference check ", difference);
无法实现上述结果,因为 page1.asin === page2.asin
不匹配,您可以 console.log(page1.asin === page2.asin)
并理解差异,因为我无法解释。
我正在努力实现:- 将 B 数组与 A 数组进行比较并从数组 B 中获取不匹配的对象。
面临的困难:- 无法检查值是否存在于数组 A - 来自数组 B 的 sku 数组 - sku 值。
只有 2 行
将A数组设为一级数组-这样会更容易找到匹配项
A.map(i => i.portfolioData.portfolioData).flat();
过滤找到的元素
B.ProductMetadataList .filter(i => !assinListA.find(j => i.asin === j.asin && j.sku.includes(i.sku)))
const A = [
{
campaignId: "3964126695264",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
{
campaignId: "137244687845173",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
{
campaignId: "182862063246935",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
{
campaignId: "262747542546680",
portfolioData: {
portfolioData: [
{
asin: "B08FHG17ZH",
sku: ["130511"],
},
{
asin: "B08FHF74TB",
sku: ["130512"],
},
{
asin: "B08FHBWP89",
sku: ["130513"],
},
{
asin: "B08FHFL6GD",
sku: ["130514"],
},
{
asin: "B08FHDWTK2",
sku: ["130515"],
},
{
asin: "B08FHFMW1V",
sku: ["130516"],
},
{
asin: "B08FHF6V8B",
sku: ["130517"],
},
{
asin: "B08FHFWDVM",
sku: ["130518"],
},
{
asin: "B08FHFD18R",
sku: ["130519"],
},
{
asin: "B08JKK4217",
sku: ["130520"],
},
{
asin: "B08JKP82PB",
sku: ["130521"],
},
],
},
},
];
const B = {
ProductMetadataList: [
{
asin: "B08FHG17ZH",
sku: "130511"
},
{
asin: "B08FHF74TB",
sku: "130512"
},
{
asin: "B08FHBWP89",
sku: "130513"
},
{
asin: "B08FHFL6GD",
sku: "130514"
},
{
asin: "B08FHDWTK2",
sku: "130515"
},
{
asin: "B08FHFMW1V",
sku: "130516"
},
{
asin: "B08FHF6V8B",
sku: "130517"
},
{
asin: "B08FHFWDVM",
sku: "130518"
},
{
asin: "B08FHFD18R",
sku: "130519"
},
{
asin: "B08JKK4217",
sku: "130520"
},
{
asin: "B08JKP82PB",
sku: "130521"
},
{
asin: "B08JKP82PB",
sku: "13052221"
},
],
};
const assinListA = A.map(i => i.portfolioData.portfolioData).flat();
const res = B.ProductMetadataList
.filter(i => !assinListA.find(j => i.asin === j.asin && j.sku.includes(i.sku)))
console.log(res)
一种简单易读的方法是注册匹配数据 array B
的索引,然后根据这些索引进行过滤。
注意:我减少了数组以使代码更易于阅读。它将与您的数据一起使用。
const A = [
{
campaignId: "3964126695264",
portfolioData: {
portfolioData: [
{ asin: "B08JKK4217", sku: ["130520"]},
{ asin: "B08JKP82PB", sku: ["130521"]}
]
}
},
{
campaignId: "137244687845173",
portfolioData: {
portfolioData: [
{ asin: "B08JKK4217", sku: ["130520"]},
{ asin: "B08JKP82PB", sku: ["130521"]}
]
}
}
]
const B = {
ProductMetadataList: [
{ asin: "B08JKK4217", sku: "130520"},
{ asin: "B08JKP82PB", sku: "130521"},
{ asin: "B08JKP82PB", sku: "13052221"}
]
}
let indexes = [] // matching indexes of array B
for(item of A){
let portfolioData = item['portfolioData']['portfolioData']
portfolioData.forEach((el_A) => {
B.ProductMetadataList.forEach((el_B, i) => {
// conditions
let C1 = el_A.asin === el_B.asin
let C2 = el_A['sku'].join() === el_B['sku']
let C3 = !indexes.includes(i)
// save index if there is a match and the index has not been saved before
if(C1 && C2 && C3) indexes.push(i)
})
})
}
let result = B.ProductMetadataList.filter((el,index) => !indexes.includes(index))
console.log(result)