Object.length在javascript中未定义
这个问题在这里已经有了答案:
因为coordinates
是Object
not Array
,所以用于for..in
var coordinates = {
"a": [
[1, 2],
[8, 9],
[3, 5],
[6, 1]
],
"b": [
[5, 8],
[2, 4],
[6, 8],
[1, 9]
]
};
for (var i in coordinates) {
console.log(coordinates[i])
}
或Object.keys
var coordinates = {
"a": [
[1, 2],
[8, 9],
[3, 5],
[6, 1]
],
"b": [
[5, 8],
[2, 4],
[6, 8],
[1, 9]
]
};
var keys = Object.keys(coordinates);
for (var i = 0, len = keys.length; i < len; i++) {
console.log(coordinates[keys[i]]);
}
coordinates
是一个对象。 JavaScript中的对象默认情况下不具有length
属性。 一些对象具有长度属性:
"a string - length is the number of characters".length
['an array', 'length is the number of elements'].length
(function(a, b) { "a function - length is the number of parameters" }).length
你可能试图找到你的对象中的keys
的数量,这可以通过Object.keys()
:
var keyCount = Object.keys(coordinates).length;
要小心,因为length
属性可以添加到任何对象:
var confusingObject = { length: 100 };
http://jsfiddle.net/3wzb7jen/2/
alert(Object.keys(coordinates).length);
链接地址: http://www.djcxy.com/p/27259.html
上一篇: Object.length undefined in javascript
下一篇: Check Length of Multidimensional Arrays with Javascript