Reputation: 3159
I have 2 arrays:
a = [{name:"test3", input:[{val:3}]}, {name:"ss", input:[{val:84}]},{name:"sss", input:[{val:86}]},{name:"test", input:[{val:6}]}, {name:"some", input:[{val:8}]}]
a2 = [{name: "dd", field:3},{name: "dd", field:6}]
Now I'm trying to get unique values from both the arrays using `
filter
:
a.forEach(function(i){
i.input.forEach(function(j){
a2.filter(function(k){return j.val !== k.field;});
});})
and then I want to use: forEach(function(p){p.remove()}); //remove the unique values
so I'm comaparing the val
value from first array to the field
value in the second one:
the expected outcome:
[{name:"ss", input:[{val:84}]},{name:"sss", input:[{val:86}]},{name:"some", input:[{val:8}]}] // these are the ones whose `val` from `a` does not match with the `field` from `a2`
The above code returns nothing, any idea what could be missing?
Upvotes: 1
Views: 636
Reputation: 11958
Filter the first array based on finding if an element with that input val exists in the second array through a double filter:
let a = [{name:"test3", input:[{val:3}]}, {name:"ss", input:[{val:84}]},{name:"sss", input:[{val:86}]},{name:"test", input:[{val:6}]}, {name:"some", input:[{val:8}]}]
let a2 = [{name: "dd", field:3},{name: "dd", field:6}]
let results = a.filter(e => {
return a2.filter(q => q.field === e.input['val']).length < 1;
});
console.log(results)
Upvotes: 1
Reputation: 3475
I think there is a simpler approach:
const a = [{name: "test3", input: [{val: 3 }] }, {name: "ss", input: [{val: 84 }] }, {name: "sss", input: [{val: 86 }] }, {name: "test", input: [{val: 6 }] }, {name: "some", input: [{val: 8 }] } ];
const a2 = [{name: "dd", field: 3 }, {name: "dd", field: 6 }];
const filterBy = a2.map(v => v.field); // [3, 6]
const res = a.filter(el => !el.input.some(input => filterBy.includes(input.val)))
console.log(res)
Upvotes: 3