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

210
Views
How to loop many objects inside array to collect a specific item?

I have this object:

data: [
  {
    name: bed,
    price: [
      {
        id: 2,
        color: red
      },
      {
        id: 4,
        color: blue
      }
    ]
  },
  {
    name: lego,
    price: [
      {
        id: 5,
        color: red
      },
      {
        id: 6,
        color: blue
      }
    ]
  },
  {
    name: mouse,
    price: [
      {
        id: 7,
        color: red
      },
      {
        id: 9,
        color: blue
      }
    ]
  }
]

I want to collect the id prices of each object like this:

ids: [ [ 2, 4 ], [ 5, 6 ], [ 7, 9 ] ]

I tried this but it doesn’t work:

ids() {
  const datas = this.data;
  let idPrices = null;
  let prices = [];
  
  for (let priceId in datas) {
    prices.push(priceId.price);
  }
  
  idPrices = prices.map((prices) => prices.map((price) => price.id));
  
  return idPrices;
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use Array.map:

const data=[{name:"bed",price:[{id:2,color:"red"},{id:4,color:"blue"}]},{name:"lego",price:[{id:5,color:"red"},{id:6,color:"blue"}]},{name:"mouse",price:[{id:7,color:"red"},{id:9,color:"blue"}]}];

const ids = data.map(e => e.price.map(f => f.id));
console.log(ids);

about 4 years ago · Juan Pablo Isaza Report

0

Maybe this one helps you? I think it's possible to do it more simple but this is the best i can do at 1:30AM. :)

const data = [
      {name: 'bed', price:[{id: 2 ,color: 'red'}, {id: 4, color: 'blue'}]},
      {name: 'lego', price:[{id: 5 ,color: 'red'}, {id: 6, color:'blue'}]},
      {name: 'mouse', price:[{id: 7 ,color: 'red'}, {id: 9, color: 'blue'}]}
      ]
      
      
let idPrices = [];
for (const i in data) {
    let prices = [];

 for (const j in data[i].price) {
  prices.push(data[i].price[j].id)

}
  idPrices.push(prices)

}
console.log(idPrices);

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!