0

Let say I have the following JSON

results = results.map(result => _.pick(result, ['id', 'vessel_type.id', 'emails[0].id'])); but only able to get the first email id

How do I get all email id without knowing the length of email array prior?

Is there something like results = results.map(result => _.pick(result, ['id', 'vessel_type.id', 'emails[*].id']));???

{
    "results": [
        {
            "name": "VESSELRARARARARA",
            "id": 2213,
            "vessel_type": {
                "id": 33
            },
            "emails": [
                {
                    "id": 4733
                },
                {
                    "id": 4788
                }
            ]
        }
    ],
    "totalCount": 555
}

I need to get id, vessel_type.id and all email id

{
    "results": [
        {
            "name": "VESSELRARARARARA",
            "id": 2213,
            "emails": [
                {
                    "id": 4733
                },
                {
                    "id": 4788
                }
            ]
        }
    ],
    "totalCount": 555
}


1 Answer 1

0

You can do it without lodash, like this:

const dat={
"results": [
    {
        "name": "VESSELRARARARARA",
        "id": 2213,
        "vessel_type": {
            "id": 33
        },
        "emails": [
            {
                "id": 4733
            },
            {
                "id": 4788
            }
        ]
    }
],
"totalCount": 555
};

const res=dat.results.map(o=>
 ({id:o.id,vessel_type:o.vessel_type.id,
 emails:o.emails.map(m=>m.id)}));

console.log(res);

1
  • That object got more than 100 fields, I can get most of them via lodash, and I want to get all of them via lodash becoz of consistency. Sorry, this is not a good answer
    – J L
    Commented Sep 3, 2022 at 15:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.