I have the following object and I want to filter it based on some properties and output only some parts of it.
{
"email" : "[email protected]",
"name" : " John Doe",
"groups" : [
{
"name" : "group1",
"country": "US",
"contacts" : [
{ "localId" : "c1", "address" : "some address 1" },
{ "localId" : "c2", "address" : "some address 2" },
{ "localId" : "c3", "address" : "some address 3" }
]
},
{
"name" : "group2",
"country": "Canada",
"contacts" : [
{ "localId" : "c1", "address" : "some address 1" },
{ "localId" : "c3", "address" : "some address 3" }
]
}
]
}
the result should look like:
{
"email" : "[email protected]",
"name" : " John Doe",
"groups" : [
{
"name" : "group1",
"country": "US",
"contacts" : [
{
"localId" : "c3",
"address" : "some address 3"
}
]
}
]
}
So my conditions are:
groups.name="group1"
groups.contacts.localId="c3"
how can I achieve it using some ecma6 js function? with the least memory load? I am in nodejs env >=8.9.0.
update:
here is my failing attempt:
const conditions = {"groups.name": "group1", "groups.contacts.localId": "c3"};
let res = mylist.map((i)=>{
return {
email: i.email,
name: i.name,
groupsName: conditions.groups.name
}
})