0

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
  }

})
2
  • Please show what you have tried. Stackoverflow isn't a free code writing service. The objective is to help you fix your code Commented Jan 7, 2018 at 1:36
  • @charlietfl please see the updated question wit my failing attempt. Commented Jan 7, 2018 at 1:43

2 Answers 2

1

You can do this fairly succinctly with filter(). If you filter on group name first you won't waste time filtering the contacts:

let obj = {
  "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"
        }
      ]
    }
  ]
}

let newObj = {
  "email": obj.email,
  "name": obj.name,
  "groups": obj.groups.filter(item => item.name == "group1").map(g => (g.contacts = g.contacts.filter(c => c.localId == "c3"), g))
}
console.log(newObj)

Sign up to request clarification or add additional context in comments.

2 Comments

what is the comma g at the end of the filter/map line?
Hi @Bonnard, that's the comma operator. It makes g the return value of the map. More here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
0

You can use filter and map. If obj is your initial object then you can do:

obj.groups = obj.groups.filter((g)=>g.name==="group1")
for(int i = 0; i < obj.groups.length;i++)
{
    obj.groups[i].contacts = obj.groups[i].contacts.filter((c)=>c.localId==="c3"))
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.