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?
1 Answer
Reset to default 6You 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);