I'm working on a Aura component and getting below array as response.getReturnValue() from server

[{"Id":"0QLDM000000gpJ24AI","Product2Id":"01t8W000005n3SPQAY","Product2":{"Name":"*à confirmer","Id":"01t8W000005n3SPQAY"}},

I need only 2 key-value pair(s) out of this -

Id, Name

So basically I need a new array with below format to proceed further

[{"Id":"0QLDM000000gpJ24AI","Name":"*à confirmer"},

I've already tried most of the javascript method showing online but no help. Please help me out on this.

2 Answers 2


you can use Array.map() method to transform the original array into a new array with only the desired key-value pairs.

const response = [{"Id":"0QLDM000000gpJ24AI","Product2Id":"01t8W000005n3SPQAY","Product2":{"Name":"*à confirmer","Id":"01t8W000005n3SPQAY"}}, {"Id":"0QLDM000000gpJ74AI","Product2Id":"01t8W000005mt3JQAQ","Product2":{"Name":"1101001603S","Id":"01t8W000005mt3JQAQ"}}];

const result = response.map(({ Id, Product2: { Name } }) => ({ Id, Name }));


The map() method will iterate over each object in the original array. For each object, we're creating a new object with only the Id and Name properties that we want. We then return this new object from the map() function, which builds up the new array for us.

  • Hey welcome to SFSE! Thanks for your contributions, keep it up and see you around!
    – Raul
    Commented Mar 9, 2023 at 13:17
  • Thanks Raul, will try!
    – Aman Patil
    Commented Mar 9, 2023 at 15:15

You can use map method to get desired JSON result:

const products = [
        Id: "0QLDM000000gpJ24AI",
        Product2Id: "01t8W000005n3SPQAY",
        Product2: { Name: "*à confirmer", Id: "01t8W000005n3SPQAY" },
        Id: "0QLDM000000gpJ74AI",
        Product2Id: "01t8W000005mt3JQAQ",
        Product2: { Name: "1101001603S", Id: "01t8W000005mt3JQAQ" },

// Pass a function to map
const modifiedProducts = products.map((p) => {
    return { Name: p.Product2.Name, Id: p.Id };

// Expected output: Array [{"Name":"*à confirmer","Id":"0QLDM000000gpJ24AI"},{"Name":"1101001603S","Id":"0QLDM000000gpJ74AI"}]
  • Thank you for your prompt response. But I want the outer ID value(s) not the Product2 ones. Like [{"Id":"0QLDM000000gpJ24AI","Name":"*à confirmer"}, {"Id":"0QLDM000000gpJ74AI","Name":"1101001603S"}] Id's are outer Id's, not the inner ones. Commented Mar 9, 2023 at 12:53
  • Such a life saviour. Thanks a lot. Commented Mar 9, 2023 at 13:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .