4

I have a hashed object where the keys are added dynamically through user selections.

I want to iterate over it and extract the values similar to the way I would do if it was simply an array: selections.map(cart => /*do stuff*/).

How can I achieve this?

S. Schenk
  • 1,750
  • 2
  • 20
  • 42

1 Answers1

5

Use Object.keys()

The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

var array = Object.keys(selections).map(k => selections[k]);
// get all values from the object
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358