I don’t understand because I use the method “find” but I get “undefined”… My data :
JavaScript
x
19
19
1
[
2
{ "id": 2, "title": "My project", "nameStructure": "Entreprise", "studies":
3
[
4
{"id": 3, "name": "My stidue", "status": "in prepa" },
5
{ "id": 4, "name": "My second study ", "status": "In"}
6
],
7
"typeStructure": "Entreprise"
8
},
9
{ "id": 3, "title": "My project 2", "nameStructure": "Entreprise 2", "studies":
10
[
11
{"id": 4, "name": "My stidue 2", "status": "in prepa" },
12
{ "id": 5, "name": "My second study 2 ", "status": "In"}
13
],
14
"typeStructure": "Entreprise 2"
15
},
16
17
]
18
19
I would like to have only the object with the ID 2 for example.
So I wrote :
JavaScript
1
3
1
const id = 2
2
myarray.filter(p => p.id === id);
3
But it does not work… I always get “undefined”
Thanks for help
Advertisement
Answer
ID is a number, therefore you need to remove the quotes around 2
JavaScript
1
2
1
myarray.filter(p => p.id === 2);
2
and the operator === in Javascript means that 2 should be equal to “2” as in value and type
reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality