最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if property of an objects of array matches with one of the values in another array of object - Stack O

programmeradmin2浏览0评论

I know the header is way too plicated but that's how it is right now. Let me make it more clear for you.

There is an object like

const users= [
{
id:"1",
name:"John",
mail:"[email protected]",
},
{
id:"2",
name:"Joe",
mail:"[email protected]",
},
]

Then there is

const attendingUsers = [
{
id:"1",
name:"John",
mail:"[email protected]",
.....
},
{
id:"2",
name:"Joe",
mail:"[email protected]",
.....
},}
] 

Both of the arrays has different properties and I just want to get the ones that are important to me. What I need is to look through users array and find the ones that has the same ids as the ones from attendingUsers.

I have came up with users.filter(user => user.id == attendingUsers.map(attendingUser => attendingUser.id)); but that is simply returning empty array. Any idea what would be the best way to tackle this sort of a problem?

I know the header is way too plicated but that's how it is right now. Let me make it more clear for you.

There is an object like

const users= [
{
id:"1",
name:"John",
mail:"[email protected]",
},
{
id:"2",
name:"Joe",
mail:"[email protected]",
},
]

Then there is

const attendingUsers = [
{
id:"1",
name:"John",
mail:"[email protected]",
.....
},
{
id:"2",
name:"Joe",
mail:"[email protected]",
.....
},}
] 

Both of the arrays has different properties and I just want to get the ones that are important to me. What I need is to look through users array and find the ones that has the same ids as the ones from attendingUsers.

I have came up with users.filter(user => user.id == attendingUsers.map(attendingUser => attendingUser.id)); but that is simply returning empty array. Any idea what would be the best way to tackle this sort of a problem?

Share Improve this question edited Dec 17, 2020 at 15:27 em_code asked Dec 17, 2020 at 15:16 em_codeem_code 4381 gold badge7 silver badges20 bronze badges 1
  • 3 user.id is a number and attendingUsers.map(attendingUser => attendingUser.id) is an array of strings, they're not going to be equal (unless the array has only one value and that is equal to the string of the ID, because of the way JS stringifies arrays and the non-strict parison ==). You presumably want to know if user.id is in that array, not equal to it. – jonrsharpe Commented Dec 17, 2020 at 15:19
Add a ment  | 

2 Answers 2

Reset to default 4

First off, you'll have to make sure if the ID types are correct. Users has Number type for IDs but attendingUsers has String type.

Let's say they're both the same type for the code below (I'm going with string).

You can turn the attendingUsers array into an array of strings with:

const attendingUsersIds = attendingUsers.map(attendingUser => attendingUser.id)

Then match the ids with:

const matchedUsers = users.filter(user => attendingUsersIds.includes(user.id))

If they're intended to not be the same type, you can use user.id.toString() to turn the Number into a String or parseInt(attendingUser.id) to turn the String into a Number.

We can use Array.map to create a new array of users, with a property isAttending added to each user.

We determine if they are attending by using Array.some to search for any matching attendee with the same id.

const users = [
    {
        id:1,
        name:"John",
        mail:"[email protected]",
    },
    {
        id:2,
        name:"Joe",
        mail:"[email protected]",
    },
    {       
        id:3,
        name:"Alice",
        mail:"[email protected]",
    }
]

const attendingUsers = [
    {
        id:"1",
        name:"John",
        mail:"[email protected]",

    },
    {
        id:"2",
        name:"Joe",
        mail:"[email protected]",
    }
] 

const result = users.map(user => { 
    return { ...user, isAttending: attendingUsers.some(({id}) => id == user.id) };
});
console.log("Users (with attending property):", result);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论