Searched and tried and no luck so far.
搜索和尝试,到目前为止没有运气。
var newUsers = [{name: 'rich', id: 25}, {name: 'lauren', id: 35}, {name: 'dave', id: 28} ]
var likedUsers = [{name: 'derek', id: 39}, {name: 'rich', id: 25}, {name: 'brian', id: 38} ]
What I want returned is:
我想要的是:
var leftUsers = [{name: 'lauren', id: 35}, {name: 'dave', id: 28} ]
basically without the rich
object as this is a duplicate. I only care about the id
key.
基本上没有富对象,因为这是重复的。我只关心id键。
I have tried:
我努力了:
newUsers.forEach((nUser) => {
likedUsers.forEach((lUser) => {
if (nUser.id !== lUser.id){
leftUsers.push(nUser)
}
})
})
but obviously this won't work as this will just add them all as soon as they don't match.
但显然这不会起作用,因为只要它们不匹配就会将它们全部添加。
if possible would like an es6 solution using forEach/map/filter
如果可能的话,想要使用forEach / map / filter的es6解决方案
thanks
谢谢
4 个解决方案
#1
2
With array.prototype.filter
to filter out items that exists in likedUsers
and array.prototype.findIndex
to check the existence, it should be:
使用array.prototype.filter过滤掉preferUsers和array.prototype.findIndex中存在的项以检查是否存在,它应该是:
var newUsers = [{name: 'rich', id: 25}, {name: 'lauren', id: 35}, {name: 'dave', id: 28} ];
var likedUsers = [{name: 'derek', id: 39}, {name: 'rich', id: 25}, {name: 'brian', id: 38} ];
var leftUsers = newUsers.filter(u => likedUsers.findIndex(lu => lu.id === u.id) === -1);
console.log(leftUsers);
#2
2
You can do this with filter()
and some()
methods.
您可以使用filter()和some()方法执行此操作。
var newUsers = [{name: 'rich', id: 25}, {name: 'lauren', id: 35}, {name: 'dave', id: 28} ]
var likedUsers = [{name: 'derek', id: 39}, {name: 'rich', id: 25}, {name: 'brian', id: 38} ]
const result = newUsers.filter(e => !likedUsers.some(a => a.id == e.id));
console.log(result)
#3
0
var newUsers = [{name: 'rich', id: 25}, {name: 'lauren', id: 35}, {name: 'dave', id: 28} ];
var likedUsers = [{name: 'derek', id: 39}, {name: 'rich', id: 25}, {name: 'brian', id: 38} ];
var leftusers = newUsers.filter( item => !likedUsers.find(item2 => item.id == item2.id));
console.log(leftusers);
#4
0
You can create a Set of ids that are found in likedUsers
, and filter the newUsers
by checking if an id is in the Set:
您可以创建在preferUsers中找到的一组ID,并通过检查ID是否在Set中来过滤newUsers:
const newUsers = [{name: 'rich', id: 25}, {name: 'lauren', id: 35}, {name: 'dave', id: 28} ]
const likedUsers = [{name: 'derek', id: 39}, {name: 'rich', id: 25}, {name: 'brian', id: 38} ]
const result = newUsers.filter(function({ id }) {
return !this.has(id) // take all users which ids is not found in the set
}, new Set(likedUsers.map(({ id }) => id))) // create a set of ids in likedUsers and assign to this
console.log(result)
#1
2
With array.prototype.filter
to filter out items that exists in likedUsers
and array.prototype.findIndex
to check the existence, it should be:
使用array.prototype.filter过滤掉preferUsers和array.prototype.findIndex中存在的项以检查是否存在,它应该是:
var newUsers = [{name: 'rich', id: 25}, {name: 'lauren', id: 35}, {name: 'dave', id: 28} ];
var likedUsers = [{name: 'derek', id: 39}, {name: 'rich', id: 25}, {name: 'brian', id: 38} ];
var leftUsers = newUsers.filter(u => likedUsers.findIndex(lu => lu.id === u.id) === -1);
console.log(leftUsers);
#2
2
You can do this with filter()
and some()
methods.
您可以使用filter()和some()方法执行此操作。
var newUsers = [{name: 'rich', id: 25}, {name: 'lauren', id: 35}, {name: 'dave', id: 28} ]
var likedUsers = [{name: 'derek', id: 39}, {name: 'rich', id: 25}, {name: 'brian', id: 38} ]
const result = newUsers.filter(e => !likedUsers.some(a => a.id == e.id));
console.log(result)
#3
0
var newUsers = [{name: 'rich', id: 25}, {name: 'lauren', id: 35}, {name: 'dave', id: 28} ];
var likedUsers = [{name: 'derek', id: 39}, {name: 'rich', id: 25}, {name: 'brian', id: 38} ];
var leftusers = newUsers.filter( item => !likedUsers.find(item2 => item.id == item2.id));
console.log(leftusers);
#4
0
You can create a Set of ids that are found in likedUsers
, and filter the newUsers
by checking if an id is in the Set:
您可以创建在preferUsers中找到的一组ID,并通过检查ID是否在Set中来过滤newUsers:
const newUsers = [{name: 'rich', id: 25}, {name: 'lauren', id: 35}, {name: 'dave', id: 28} ]
const likedUsers = [{name: 'derek', id: 39}, {name: 'rich', id: 25}, {name: 'brian', id: 38} ]
const result = newUsers.filter(function({ id }) {
return !this.has(id) // take all users which ids is not found in the set
}, new Set(likedUsers.map(({ id }) => id))) // create a set of ids in likedUsers and assign to this
console.log(result)