How to iterate through an object of objects, in which the keys are different and the values contains strings and objects/ arrays?

{
  "ahv1234jb": {
    "providerId": 16,
    "model": {
      "id": 0,
      "name": "Tata Intra V10",
      "make": "TATA Motors",
      "type": "SMALL",
      "bodyWeight": 0,
      "weightCapacity": 0,
      "length": 0,
      "breadth": 0,
      "height": 0,
      "imageUrl": ""
    },
    "status": "AVAILABLE",
    "yearOfManufacturing": 2004,
    "rating": {
      "sum": 5,
      "count": 1
    }
  },
  "34567jvb": {
    "providerId": 18,
    "model": {
      "id": 0,
      "name": "Tata Intra V10",
      "make": "TATA Motors",
      "type": "SMALL",
      "bodyWeight": 0,
      "weightCapacity": 0,
      "length": 0,
      "breadth": 0,
      "height": 0,
      "imageUrl": ""
    },
    "status": "AVAILABLE",
    "yearOfManufacturing": 2006,
    "rating": {
      "sum": 5,
      "count": 1
    }
  }
}

what have you tried so far? are you able to do this with a flat object?

I have tried for each loop with the object.entries taking out the values -->

Object.entries(res.data).forEach(([key, value]) => {
  let vehicles = new Array(value)
  console.log(vehicles)
})

just taking out the values I’m creating a new array for each…

It’s giving out result like this -->

Now how should i proceed as I need all these in a single array. i.e.- array of objects

Please help :blush: asap

If you just want an array of objects which are the values of each key in the main object, why don’t you just use Object.values() directly?

const values = Object.values(obj);

console.log(values);

[ { providerId: 16,
    model: 
     { id: 0,
       name: 'Tata Intra V10',
       make: 'TATA Motors',
       type: 'SMALL',
       bodyWeight: 0,
       weightCapacity: 0,
       length: 0,
       breadth: 0,
       height: 0,
       imageUrl: '' },
    status: 'AVAILABLE',
    yearOfManufacturing: 2004,
    rating: { sum: 5, count: 1 } },
  { providerId: 18,
    model: 
     { id: 0,
       name: 'Tata Intra V10',
       make: 'TATA Motors',
       type: 'SMALL',
       bodyWeight: 0,
       weightCapacity: 0,
       length: 0,
       breadth: 0,
       height: 0,
       imageUrl: '' },
    status: 'AVAILABLE',
    yearOfManufacturing: 2006,
    rating: { sum: 5, count: 1 } } ]

Unless you’re looking for something else?

1 Like

Bhai Bhai Bhai Bhai

Thank you soo much bro

LOL! evergreen meme!