Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data from nested objects in the form of array

As you can see, the keys are now the name of the items and the corresponding value is another object consisting of two keys - quantity and price.

    var itemsToBuy = {
    milk: {
        quantity : 5,
        price: 20
    },
    bread: {
        quantity : 2,
        price: 15
    },
    potato: {
        quantity : 3,
        price: 10
    }
}

I tried but I am getting only undefined output. Want to get data such like :

['milk', 'bread', 'potato']

[20, 15, 10]
like image 917
Nitesh Singh Avatar asked Dec 17 '22 11:12

Nitesh Singh


1 Answers

const object1  = {
  milk: {
      quantity : 5,
      price: 20
  },
  bread: {
      quantity : 2,
      price: 15
  },
  potato: {
      quantity : 3,
      price: 10
  }
}
console.log(Object.entries(object1).map(([key,value])=> value.price))
like image 103
MUHAMMAD ILYAS Avatar answered Dec 20 '22 02:12

MUHAMMAD ILYAS