开发者

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.

But what if the object is an array and I want the list of integer indexes that it has?

Is there a simple way to extract this 开发者_开发百科without having to 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;
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜