在javascript中未定义Object.length

时间:2015-06-16 07:45:19

标签: javascript arrays object

我有一个像

这样的数组的javascript对象
ProductPart.objects.filter(part=v)

var coordinates = { "a": [ [1, 2], [8, 9], [3, 5], [6, 1] ], "b": [ [5, 8], [2, 4], [6, 8], [1, 9] ] }; 返回undefined。 Fiddle is here

3 个答案:

答案 0 :(得分:13)

那是因为coordinates Object不是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]]);
}

答案 1 :(得分:8)

coordinates是一个对象。默认情况下,javascript中的对象不具有length属性。某些对象具有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 };

答案 2 :(得分:2)

http://jsfiddle.net/3wzb7jen/2/

while( (fscanf(fp,"%d",&num)) != EOF) // This is the line 46