Converting array to hash

Hi guys!

So I’ve got this cartItems object which lists my cart items. Each cart item has a ‘name’ and ‘quantity’ property.
I want to create an object like this…

{
    {name: cartItem1,
    quantity: 1},
    {name: cartItem2,
    quantity: 3}
}

I tried a map, which got me a nested array like so…

[[cartItem1, 1], [cartItem2, 3]]

But I don’t know how to then convert this into the object above.

What’s the best way to go about this? It’s making my head hurt!

An object is a data structure made up of property-value pairs. You can’t have values without properties. You can create an object like:

{
   property1:  {name: cartItem1,  quantity: 1},
   property2:  {name: cartItem2,  quantity: 3}
}

The object you have pointed out above will lead to an error if I am not mistaken unless you change it to an array. That is if you don’t want to add properties, so that it becomes:

[
    {name: cartItem1,  quantity: 1},
    {name: cartItem2,  quantity: 3}
]

Are you creating the above object from scratch or from another dataset? If it is from scratch why have you chosen to go with an object? It appears an array of objects would be the best option.