检查 javascript 数组中的数字索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17245397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking for a numerical index in a javascript array
提问by jkushner
I'm receiving json data that is aggregated by numerical indexes.
我正在接收按数字索引聚合的 json 数据。
When I'm in my forloop, for example, the index might start at 1, which means in my forloop an error would occur because 0 doesnt exist.
例如,当我在 forloop 中时,索引可能从 1 开始,这意味着在我的 forloop 中会发生错误,因为 0 不存在。
How do I check if a numerical index exists in the javascript array?
如何检查javascript数组中是否存在数字索引?
回答by andlrc
var a = [1, 2, 3], index = 2;
if ( a[index] !== void 0 ) { /* void 0 === undefined */
/* See concern about ``undefined'' below. */
/* index doesn't point to an undefined item. */
}
回答by JasonM
You should be able to use for(key in data)
你应该可以使用 for(key in data)
var data = [];
data[1] = 'a';
data[3] = 'b';
for(var index in data) {
console.log(index+":"+data[index]);
}
//Output:
// 1-a
// 3-b
Which will loop over each key item in data if the indexes aren't contiguous.
如果索引不连续,它将遍历数据中的每个关键项。
回答by Xotic750
If what you are actually describing is an Object
rather than an Array
, but is array like in the fact that it has properties that are of uint32_tbut does not have essential length
property present. Then you could convert it to a real array like this. Browser compatibility wise this requires support of hasOwnProperty
如果您实际描述的是 anObject
而不是 an Array
,而是类似于数组,因为它具有uint32_t 的属性但不存在基本length
属性。然后你可以把它转换成这样一个真正的数组。浏览器兼容性明智这需要支持hasOwnProperty
Javascript
Javascript
function toArray(arrayLike) {
var array = [],
i;
for (i in arrayLike) {
if (Object.prototype.hasOwnProperty.call(arrayLike, i) && i >= 0 && i <= 4294967295 && parseInt(i) === +i) {
array[i] = arrayLike[i];
}
}
return array;
}
var object = {
1: "a",
30: "b",
50: "c",
},
array = toArray(object);
console.log(array);
Output
输出
[1: "a", 30: "b", 50: "c"
]`
[1: "a", 30: "b", 50: "c"
]`
On jsfiddle
Ok, now you have a sparsely populated array and want to use a for
loop to do something.
好的,现在您有一个稀疏填充的数组,并且想要使用for
循环来做某事。
Javascript
Javascript
var array = [],
length,
i;
array[1] = "a";
array[30] = "b";
array[50] = "c";
length = array.length;
for (i = 0; i < length; i += 1) {
if (Object.prototype.hasOwnProperty.call(array, i)) {
console.log(i, array[i]);
}
}
Ouput
输出
1 "a"
30 "b"
50 "c"
On jsfiddle
Alternatively, you can use Array.prototype.forEach
if your browser supports it, or the available shim as given on the MDN page that I linked, or es5_shim
或者,Array.prototype.forEach
如果您的浏览器支持它,您可以使用它,或者我链接的 MDN 页面上给出的可用垫片,或es5_shim
Javascript
Javascript
var array = [];
array[1] = "a";
array[30] = "b";
array[50] = "c";
array.forEach(function (element, index) {
console.log(index, element);
});
Output
输出
1 "a"
30 "b"
50 "c"
On jsfiddle