javascript - How to get data from nested objects in the form of array - Stack Overflow

As you can see, the keys are now the name of the items and the corresponding value is another object co

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]

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]
Share Improve this question edited Feb 1, 2021 at 11:58 Nitesh Singh asked Jan 9, 2021 at 10:37 Nitesh SinghNitesh Singh 3951 gold badge3 silver badges16 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 5

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))

Solution

  • With Object.keys() you can get the keys of your Object as an array. -> ['milk', 'bread', 'potato']

  • With Object.entries() in bination with map() you can get the values of the price property as an array. -> [20, 15, 10]

Example:

     var itemsToBuy = {
        milk: {
            quantity : 5,
            price: 20
        },
        bread: {
            quantity : 2,
            price: 15
        },
        potato: {
            quantity : 3,
            price: 10
        }
    }
    const keys = Object.keys(itemsToBuy);
    const val = Object.entries(itemsToBuy).map((element)=> element[1].price)

    console.log(keys);
    console.log(val);
    

for names:

Object.keys(itemsToBuy) //["milk", "bread", "potato"]

and for prices:

Object.entries(itemsToBuy).map((el)=> el[1].price)//[20, 15, 10]

To get object keys you can use: Object.keys()

let keys = Object.keys(itemsToBuy) // ["milk", "bread", "potato"]

And for the nested values you can get 1st all values

let values = Object.values(itemsToBuy) // [{quantity : 5, price: 20}, {quantity : 2, price: 15}, {quantity : 3, price: 10}]

And then map to return the new array with elements you want to extract ex.price:

let price = values.map(value => value.price) // [20, 15, 10]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743623144a4480108.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信