I would like to compare the var arrayB with var arrayA in the said condition as arrayA[n].[m].id will be matched with arrayB[ele].optionValue[e].id.
var arrayA = [
[{value: "#0767b9", id: 162,productId: 1}, value: "#f4b7d4",id: 164,productId: 1],
[{value: "#44acd8",id: 102,productId: 2}],
[{value: "#609923",id: 106,productId: 3}, {value: "#ee3b70",id: 107,productId: 3}]
]
var arrayB = [
{
id: 1,
optionValue: [{value: "#002e63",id: 161,productId: 1}, {value: "#0767b9",id: 162,productId: 1},{value: "#010b1d",id: 163,productId: 1}, {value: "#f4b7d4",id: 164,productId: 1}]
},
{
id: 2,
optionValue: [{value: "#EC7063",id: 93,productId: 2}, {value: "#bf0000",id: 94,productId: 2}, {value: "#44acd8",id: 102,productId: 2}, {value: "#ffdbdb",id: 103,productId: 2}]
},
{
id: 3,
optionValue: [{value: "#d861bd",id: 105,productId: 3}, {value: "#609923",id: 106,productId: 3}, {value: "#ee3b70",id: 107,productId: 3}]
},
{
id: 4,
optionValue: [{value: "#44acd8",id: 165,productId: 4}]
}
]
My goal is to return var arrayB with that filtered data that will remove the object which is not in the var arrayA, like this:
var result = [
{
id: 1,
optionValue: [{value: "#0767b9",id: 162,productId: 1},{value: "#f4b7d4",id: 164,productId: 1}]
},
{
id: 2,
optionValue: [{value: "#44acd8",id: 102,productId: 2}]
},
{
id: 3,
optionValue: [{value: "#609923",id: 106,productId: 3},{value: "#ee3b70",id: 107,productId: 3}]
},
{
id: 4,
optionValue: [{value: "#44acd8",id: 165,productId: 4}]
}
]
I have workaround as below but that is not giving the desired output.
const myArray = arrayB.map((el) => {
el.optionValue.filter((fl) => {
arrayA.map(values => {
values.map((value) => {
!value.id.includes(fl.id)
})
})
})
});
Note: For id:4 in result set is the case that is the selected productId for which there is no any value is selected. So in arrayA there is no value for productId:4. So in result for this kind of cases if there is no values are for comparison then it should be return as it is instead of blank array.
id: 4in the result set?arrayAthere is no value forproductId:4... so in result for this kind of cases if there is no values are for comparison then it should be return as it is instead of blank array.