QuepasaTio QuepasaTio - 2 months ago 11
Javascript Question

Javascript Filter array list by id field into a new variable

I have a data list which looks like this:

{"people":[
{
"id":"100",
"name":"name 1",
"desc":"desc 1"
},
{
"id":"192",
"name":"name 2",
"desc":"desc 2"
},
{
"id":"324",
"name":"name 3",
"desc":"desc 3"
}
]}


I have it stored in a variable:

myVar

What I need to do is to cut the list down to only the data from id - 192 (for example).

So that in the end I'm left with:

{"people":[
{
"id":"192",
"name":"name 2",
"desc":"desc 2"
}
]}


So myNewVar =

{"people":[
{
"id":"192",
"name":"name 2",
"desc":"desc 2"
}
]}


How can I do this?

Answer

Should be as straight forward as just filtering it

myVar.people = myVar.people.filter( x => x.id === "192");
Comments