admin 管理员组文章数量: 1086019
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.
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
thanks
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.
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
thanks
Share Improve this question edited Jan 31, 2018 at 10:32 The Walrus asked Jan 31, 2018 at 10:31 The WalrusThe Walrus 1,2087 gold badges31 silver badges50 bronze badges 9- 1 Possible duplicate of How to merge two arrays in JavaScript and de-duplicate items – Serge K. Commented Jan 31, 2018 at 10:32
- @SergeK. well no coz this concerns objects, and 2 why are you downvoting me? – The Walrus Commented Jan 31, 2018 at 10:33
-
1
So, what happened to brian? You want only the items in
new
that aren't inliked
? – Cerbrus Commented Jan 31, 2018 at 10:34 -
1
The logic is the same... You just have to change
a[i] === a[j]
toa[i].myProp === a[j].myProp
. You could also find lot of example all over the internet. – Serge K. Commented Jan 31, 2018 at 10:35 - first merge it into one array, then loop it and save the ids in a new array var. always check the id doesnt exists in this new array and if it exists delete it on the merged one – David Commented Jan 31, 2018 at 10:35
4 Answers
Reset to default 7With array.prototype.filter
to filter out items that exists in likedUsers
and array.prototype.findIndex
to check the existence, it should be:
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);
You can do this with filter()
and some()
methods.
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)
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);
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:
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)
本文标签: Javascript return array from 2 arrays removing duplicatesStack Overflow
版权声明:本文标题:Javascript return array from 2 arrays removing duplicates - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744098652a2533372.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论