Object.length undefined in javascript
本问题已经有最佳答案,请猛点这里访问。
我有一个数组的javascript对象,比如,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | var coordinates = { "a": [ [1, 2], [8, 9], [3, 5], [6, 1] ], "b": [ [5, 8], [2, 4], [6, 8], [1, 9] ] }; |
但
因为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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]) } |
或
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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]]); } |
1 2 3 4 5 | "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 |
您可能试图在您的对象中找到
1 | var keyCount = Object.keys(coordinates).length; |
小心,因为
1 | var confusingObject = { length: 100 }; |
http://jsfiddle.net/3wzb7jen/2/
1 | alert(Object.keys(coordinates).length); |