5

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,960
  • 4
  • 25
  • 46

1 Answers1

7

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
  • 376,160
  • 25
  • 347
  • 392