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

javascript - using .includes with .map to find matching values - Stack Overflow

programmeradmin0浏览0评论

I'm trying to test if one array contains an id which exists in another array. Here's the first Array:

var ids = [
    "N06Jrz5hx6Q9bcVDBBUrF3GKSTp2",
    "eLLfNlWLkTcImTRqrYnU0nWuu9P2"
]

And here's an example of how the user object looks like:

var userExample = {
    "category": "Chemia",
    "coins": 100,
    "friends": {},
    "level": "Liceum",
    "newAnswers": false,
    "nickname": "czainusek",
    "notifications": true,
    "safety": true,
    "tagID": "drawable/icon_other1",
    "tasksUploaded": 0,
    "works": {
        "-LI1z-yO4MJnPwrdwHGl": "-LI1z-tB6EpqIhJwzchP",
        "-LIHAwCpc84UQqGeT1bU": "-LIHAw8RuTZxfgz0ef8G",
        "-LIHB2Ov5B-4XqpM79Av": "-LIHB2IPmMqOxa3UPFxs"
    },
    "id": "Nqez1dKRJ6aEpOb4BuUOK75iJoJ2"
}

And I have an array of said objects. They all look like this:

var usersExamples = [
    {
        "category": "Chemia",
        "coins": 100,
        "friends": {},
        "level": "Liceum",
        "newAnswers": false,
        "nickname": "czainusek",
        "notifications": true,
        "safety": true,
        "tagID": "drawable/icon_other1",
        "tasksUploaded": 0,
        "works": {
            "-LI1z-yO4MJnPwrdwHGl": "-LI1z-tB6EpqIhJwzchP",
            "-LIHAwCpc84UQqGeT1bU": "-LIHAw8RuTZxfgz0ef8G",
            "-LIHB2Ov5B-4XqpM79Av": "-LIHB2IPmMqOxa3UPFxs"
        },
        "id": "Nqez1dKRJ6aEpOb4BuUOK75iJoJ2"
    },
    {
        "coins": 20,
        "friends": {},
        "level": "Gimnazjum",
        "newAnswers": false,
        "nickname": "Tomasz ",
        "notifications": true,
        "safety": true,
        "tagID": "drawable/emot_icons1",
        "tasksUploaded": 5,
        "id": "SAlVGjXIagdDuJrHYO4i6LwzUql2"
    }
]

So knowing this I tried something like this:

console.log(reportIds.includes(users.map(user => user.id)));

This returned false. But if I try an id like this:

console.log(reportIds.includes('N06Jrz5hx6Q9bcVDBBUrF3GKSTp2'));

it returns true.

The object with said Id exists. I checked the ids manually. I tried this:

console.log(reportIds.includes(['8VbBNBDrT1Z3kmyaQzy6LqskcOT2', 'N06Jrz5hx6Q9bcVDBBUrF3GKSTp2'].map(id => id)));

But this returned false.

Same with this:

console.log(reportIds.includes(['8VbBNBDrT1Z3kmyaQzy6LqskcOT2', 'N06Jrz5hx6Q9bcVDBBUrF3GKSTp2']));

So how can I check which ids are in the ids array?

I'm trying to test if one array contains an id which exists in another array. Here's the first Array:

var ids = [
    "N06Jrz5hx6Q9bcVDBBUrF3GKSTp2",
    "eLLfNlWLkTcImTRqrYnU0nWuu9P2"
]

And here's an example of how the user object looks like:

var userExample = {
    "category": "Chemia",
    "coins": 100,
    "friends": {},
    "level": "Liceum",
    "newAnswers": false,
    "nickname": "czainusek",
    "notifications": true,
    "safety": true,
    "tagID": "drawable/icon_other1",
    "tasksUploaded": 0,
    "works": {
        "-LI1z-yO4MJnPwrdwHGl": "-LI1z-tB6EpqIhJwzchP",
        "-LIHAwCpc84UQqGeT1bU": "-LIHAw8RuTZxfgz0ef8G",
        "-LIHB2Ov5B-4XqpM79Av": "-LIHB2IPmMqOxa3UPFxs"
    },
    "id": "Nqez1dKRJ6aEpOb4BuUOK75iJoJ2"
}

And I have an array of said objects. They all look like this:

var usersExamples = [
    {
        "category": "Chemia",
        "coins": 100,
        "friends": {},
        "level": "Liceum",
        "newAnswers": false,
        "nickname": "czainusek",
        "notifications": true,
        "safety": true,
        "tagID": "drawable/icon_other1",
        "tasksUploaded": 0,
        "works": {
            "-LI1z-yO4MJnPwrdwHGl": "-LI1z-tB6EpqIhJwzchP",
            "-LIHAwCpc84UQqGeT1bU": "-LIHAw8RuTZxfgz0ef8G",
            "-LIHB2Ov5B-4XqpM79Av": "-LIHB2IPmMqOxa3UPFxs"
        },
        "id": "Nqez1dKRJ6aEpOb4BuUOK75iJoJ2"
    },
    {
        "coins": 20,
        "friends": {},
        "level": "Gimnazjum",
        "newAnswers": false,
        "nickname": "Tomasz ",
        "notifications": true,
        "safety": true,
        "tagID": "drawable/emot_icons1",
        "tasksUploaded": 5,
        "id": "SAlVGjXIagdDuJrHYO4i6LwzUql2"
    }
]

So knowing this I tried something like this:

console.log(reportIds.includes(users.map(user => user.id)));

This returned false. But if I try an id like this:

console.log(reportIds.includes('N06Jrz5hx6Q9bcVDBBUrF3GKSTp2'));

it returns true.

The object with said Id exists. I checked the ids manually. I tried this:

console.log(reportIds.includes(['8VbBNBDrT1Z3kmyaQzy6LqskcOT2', 'N06Jrz5hx6Q9bcVDBBUrF3GKSTp2'].map(id => id)));

But this returned false.

Same with this:

console.log(reportIds.includes(['8VbBNBDrT1Z3kmyaQzy6LqskcOT2', 'N06Jrz5hx6Q9bcVDBBUrF3GKSTp2']));

So how can I check which ids are in the ids array?

Share Improve this question asked Jul 27, 2018 at 12:20 Alex IronsideAlex Ironside 5,06914 gold badges76 silver badges134 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You can use a bination of Array#filter and Array#includes for that :

let ids = [
    "N06Jrz5hx6Q9bcVDBBUrF3GKSTp2",
    "eLLfNlWLkTcImTRqrYnU0nWuu9P2"
];

let usersExamples = [
    {
        "id": "Nqez1dKRJ6aEpOb4BuUOK75iJoJ2"
    },
    {
        "id": "SAlVGjXIagdDuJrHYO4i6LwzUql2"
    },
    {
        "id": "eLLfNlWLkTcImTRqrYnU0nWuu9P2"
    }
];

let result = usersExamples.filter(u => ids.includes(u.id));
console.log(result);

发布评论

评论列表(0)

  1. 暂无评论