将对象数组与嵌套对象数组进行比较,并获取无与伦比的对象数组
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数组与数组进行比较,并从数组B中获取无与伦比的对象。
困难: - 无法从数组B -SKU值中检查阵列A -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"
},
],
};
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);
Not able to achieve above result because the page1.asin === page2.asin
is not matching you can console.log(page1.asin === page2.asin)
and understand the difference as i am not able to explian.
I am trying to achieve:-
Comparing B array to A array and getting unmatched objects from array B.
Difficulties faicing:-
Not able to check if the value is present in array A - sku array from array B - sku value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只有2行
制作一个阵列一个级别数组 - 更容易找到匹配项
a.map(i => i.portfoliodata.portfoliodata).flat();
找到元素
b。构成元素师.filter(i =>!assinlista.find(j => i.asin ==== j.asin&&& j.sku.includes(i.sku)))
just 2 lines
make A array one level array - it will be easier to find a match
A.map(i => i.portfolioData.portfolioData).flat();
filter found element
B.ProductMetadataList .filter(i => !assinListA.find(j => i.asin === j.asin && j.sku.includes(i.sku)))
一种简单且易于阅读的方法是注册匹配数据的数组 B 的索引,然后通过这些索引进行过滤。
注意:我减少了数组以使代码更易于阅读。它将与您的数据一起使用。
A simple approach, easy to read, would be to register the indexes of the
array B
of the matching data and then filtering by these indexes.Note: I reduced the arrays to make the code easier to read. It will work with your data.