Probably the most concise way to get an array of values contained in an object is to use Object.keys and Array.prototype.map :
obj = { a: 1, b: 2, c: 3 }; values = Object.keys(obj).map(function (key) { return obj[key]; });
Otherwise, there is no standardized way to get an array of object values.
For iteration, ES6 introduces a for..of loop that will iterate over the values of the object:
continued from above: for (value of obj) { console.log(value);
ES7 plans to introduce array methods , so the generation of an array of values can be written as:
continued from above: values = [for (x of Object.keys(obj)) obj[x]];
If you already use an underscore, you can use the _.values method:
continued from above: _.values(obj);
If you need an effective implementation for this utility function, lodash source :
lodash.js v2.4.1 lines 2891-2914 function values(object) { var index = -1, props = keys(object), length = props.length, result = Array(length); while (++index < length) { result[index] = object[props[index]]; } return result; }
zzzzBov
source share