I\'ve got an object:
var obj = {
\"Mike\": 24,
\"Peter\": 23,
\"Simon\": 33,
\"Tom\": 12,
\"Frank\": 31
};
I want to cr
There's no built-in way to do this anywhere. The following does what you suggest, and may be "shortened" into more clever functional-programming versions depending on your library, but they'll all have the same efficiency.
function valuesToArray(obj) {
var result = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
result.push(obj[key]);
}
}
return result;
}
Using the Underscore lib try:
function valuesToArray(o) {
return _.pairs(o);
}
var obj = {
"Mike": 24,
"Peter": 23
//...
},
result = valuesToArray(obj);
Then the result is [ ["Mike", 24], ["Peter", 23] ];
More detail on the pairs method here: http://underscorejs.org/#pairs
Using bob.js this can be done pretty simply:
function valuesToArray(obj) {
return bob.collections.extensions.toArray.call(obj);
}