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.
CodePudding user response:
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)))
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)
CodePudding user response:
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.
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)