Javascript: get all integer keys and all values in an Array?
Object.keys(obj)
returns an Array
of strings that are the keys of an object.
parseInt()
them all?
Alternatively, is there a simple way to implement a sort of Object.values()
to get an Array
of the values (with normal Array integer keys) from an object?
You can loop the array for():
var arr = ["aaaa", "bbbb", "cccc"];
var iArr = [];
for(var i in arr)
{
iArr[i] = i;
alert(i+ " > " + arr[i]);
}
alert(iArr.length);
http://jsfiddle.net/Achilleterzo/kfLzD/
According to the final final final final draft of ES5, it seems that there is nothing like what you are looking for.
I think that all you can do is
var numericKeys = Object.keys(myObject).filter(function (key) {
return parseInt(key, 10).toString() === key;
});
精彩评论