There are a few similar questions out there, but not quite one that answers what I need to do. I have 2 arrays, both filled with objects and nothing else.
arr1 = [
{name:'one'},
{name:'two'},
{name:'three'},
]
arr2 = [
{name:'four'},
{name:'two'},
{name:'six'},
]
What I want to do is return arr2 such that it doesn't have anything that matches in arr1. For example:
filteredArr = [
{name:'four'},
{name:'six'},
]
A requirement is I want to use array.prototype.filter to achieve this. My problem is, whenever I attempt to solve this, it gives me duplicates in my returned array. Here's what I've come up with:
let state = {
one: [
{name:'one'},
{name:'two'},
{name:'three'}
],
two: [
{name:'one'},
{name:'four'},
{name:'five'},
{name:'three'}
]
}
let {one, two} = state
let newStuff = []
two.filter(s => {
one.filter(t => {
s.name !== t.name ? newStuff.push(s) : null
})
})
console.log(newStuff)
I know the logic of what it's doing and why I'm returning duplicates, but I don't know how to write it so that it simply returns state.two with all of the objects from state.one filtered out.