Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
How to retrieve items id from an array of object and put it in an array using javascript?

below is an array of object

const arr_obj = [
    {
        'id': 1,
        'items': [
            {
                'id':'1',
                'data': {
                    'id': 3,
                }
             },
             {
                 'id': '2',
                 'data': {
                     'id': 4,
                  }
              }
          ]
      },

      {
          'id': 2,
          'items': [
              {
                  'id':'3',
                  'data': {
                      'id': 5,
                  }
              },
          ]
      },
  ]

I want to retrieve the id property of items array and put it an array so the expected output is ['1','2','3']

could someone help me with this. thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

arr_obj.map(obj=>obj.items.map(item=>item.id)).flat()
  • .map loops over every item in an array and runs a transformation function on them, generating a new array.

  • .flat flattens a multi-dimensional array into a 1d array

I'd suggest looking into some of the other array methods, as they are very useful.


You could even improve this slightly with flatMap:

arr_obj.flatMap(obj=>obj.items.map(item=>item.id))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!