skip to Main Content

I have a response object that I’m wanting to extract all of the id values out, but I’m not sure the best way to go about it…

const obj = {
  "responseArray":[
    {
      "accounts":[
        {
          "products":[
            {
              "id":"123",
            }
          ]
        },
        {
          "products":[
            {
              "id":"456",
            }
          ]
        }
      ]
    },
    {
      "accounts":[
        {
          "products":[
            {
              "id":"987",
            }
          ]
        },
        {
          "products":[
            {
              "id":"654",
            }
          ]
        }
      ]
    }
  ]
}

The closest I’ve gotten is with mapping

const mapped = obj.responseArray.map(item => item?.accounts.map(acc => acc.products.map(prod => prod.id)))

// Response
// [ [ [Array], [Array] ], [ [Array], [Array] ] ]

These Array bits do actually contain the IDs I’m after, it’s just obviously not in the format I’d like…

4

Answers


  1. You can use flatMap to get a simple array instead of an array of arrays :

    const obj = {
      "responseArray":[
        {
          "accounts":[
            {
              "products":[
                {
                  "id":"123",
                }
              ]
            },
            {
              "products":[
                {
                  "id":"456",
                }
              ]
            }
          ]
        },
        {
          "accounts":[
            {
              "products":[
                {
                  "id":"987",
                }
              ]
            },
            {
              "products":[
                {
                  "id":"654",
                }
              ]
            }
          ]
        }
      ]
    }
    
    const mapped = obj.responseArray.flatMap(item => item?.accounts.flatMap(acc => acc.products.map(prod => prod.id)))
    
    console.log(mapped)
    Login or Signup to reply.
  2. With "modern" (Chrome 69, Firefox 62, Edge 79, Opera 56, Safari 12) versions of JavaScript you can Array.prototype.flatMap:

    obj.responseArray
      .flatMap(x => x.accounts)
      .flatMap(x => x.products)
      .map(x => x.id)
    // [ '123', '456', '987', '654' ]
    

    flatMap takes a function that returns an array and will map all arrays of this function into one big array, which can then be further processed.

    Login or Signup to reply.
  3. I am using the forEach to get the job done.

    const obj = {
      "responseArray": [{
          "accounts": [{
              "products": [{
                "id": "123",
              }]
            },
            {
              "products": [{
                "id": "456",
              }]
            }
          ]
        },
        {
          "accounts": [{
              "products": [{
                "id": "987",
              }]
            },
            {
              "products": [{
                "id": "654",
              }]
            }
          ]
        }
      ]
    }
    let result=[];
    obj.responseArray.forEach(o=>{
        o.accounts.forEach(account=>{
        account.products.forEach(product=>{
            result.push(product.id);
        })
      })
    })
    console.log(result)
    Login or Signup to reply.
  4. I guess what you are looking for is something like this,
    We need to use flatMap to flatten and map over the arrays:

    obj.responseArray
        .flatMap(item => item.accounts)
        .flatMap(account => account.products)
        .map(product => product.id);
    
    // response: [ '123', '456', '987', '654' ]
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search