Is there a way to convert that object into an array in javascript?
{
"0": {
"category": "Private",
"price": "5"
},
"1": {
"category": "VIP",
"price": "5"
},
"2": {
"category": "Premium",
"price": "5"
},
}
expected result :
pricing={[
{ category: "Private", price: 5 },
{ category: "VIP", price: 5 },
{ category: "Premium", price: 5 },
]}
2
Answers
Easiest way would be using
Object.values
Yes, you can convert the given object into an array in JavaScript using the
Object.values()
method.Here’s how you can achieve the expected result:
Output:
In the code above,
Object.values(obj)
is used to extract the values of the object as an array. Then, the resulting array is assigned to the pricing property of a new object, which matches the expected result.