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

89
Views
Return all objects inside nested loop in react

I want return all objects inside nested loop

const data = [{ id:1, products:[{id:1, name:'apple'}, {id:2, name:'orange'}] },
{id:2, products:[{id:3, name:'grapes'}, {id:4, name:'banana'},  {id:5, name:'dragonFruit'}] 
}]
 let result = data.map(item=>{
 item?.products?.map(row=> return row) })
console.log(result )

result should be:

[{id: 1, name: 'apple'}
{id: 2, name: 'orange'}
{id: 3, name: 'grapes'}
{id: 4, name: 'banana'}
{id: 5, name: 'dragonFruit'}
]
about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

const data = [{ id:1, products:[{id:1, name:'apple'}, {id:2, name:'orange'}] },
{id:2, products:[{id:3, name:'grapes'}, {id:4, name:'banana'},  {id:5, name:'dragonFruit'}] 
}]
let result = data.map(item=>{
    let res = item.products.map((val) =>{
    return val
  })
  return res
})

console.log(result)

check the above snippet, here inner map function return the value to the outer map and from the outer map function I'm returning the result

about 4 years ago · Santiago Gelvez Report

0

It is possible to use flatMap with map.

let result = data.flatMap(({products})=> 
    products.map(prd=> ({...prd })))

Let me show an example:

const data = [
    { id:1, 
        products:[
            {id:1, name:'apple'}, 
            {id:2, name:'orange'}
        ] 
    },
    {id:2, 
        products:[
            {id:3, name:'grapes'}, 
            {id:4, name:'banana'},  
            {id:5, name:'dragonFruit'}
        ] 
    }
]
 
let result = data.flatMap(({products})=> 
    products.map(prd=> ({ ...prd })))
 console.log(result)

about 4 years ago · Santiago Gelvez Report

0

You can use the flatMap. Read more about it at here

const data = [
    { 
        id:1, 
        products:[{id:1, name:'apple'}, {id:2, name:'orange'}] 
    },
    {
       id:2, 
       products:[{id:3, name:'grapes'}, {id:4, name:'banana'},  {id:5, name:'dragonFruit'}] 
    }
];
const flatResult = data.flatMap(({products})=> products);
console.log(flatResult)

The above will return the output as

[
  {
    "id": 1,
    "name": "apple"
  },
  {
    "id": 2,
    "name": "orange"
  },
  {
    "id": 3,
    "name": "grapes"
  },
  {
    "id": 4,
    "name": "banana"
  },
  {
    "id": 5,
    "name": "dragonFruit"
  }
]
about 4 years ago · Santiago Gelvez 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!