最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot get length of a list - Stack Overflow

programmeradmin8浏览0评论

Hay, i have the following list

var feedObjects = {
    0:[
        "url",
        "image"
    ],
    1:[
        "url",
        "image"
    ]
}

However when i try doing feedObjects.length it always returns null, any ideas?

Hay, i have the following list

var feedObjects = {
    0:[
        "url",
        "image"
    ],
    1:[
        "url",
        "image"
    ]
}

However when i try doing feedObjects.length it always returns null, any ideas?

Share Improve this question asked Jun 6, 2011 at 12:21 dottydotty 41.5k66 gold badges153 silver badges197 bronze badges 1
  • 1 It should return undefined because feedObjects does not have a length property. – RobG Commented Jun 6, 2011 at 12:26
Add a ment  | 

4 Answers 4

Reset to default 8

You have an Object ({} are the literal Object notation), not an Array, so there is no length property.

You will need to iterate over it with for ( in ), except this guarantees no ordering of the properties, unlike an Array (though in practice they generally e in the order defined).

Better still, swap { } with [ ] and use a real Array (well as close as JavaScript's arrays are to real ones).

You have declared an associative array, not an indexed array. Try this

var feedObjects = [
    [
        "url",
        "image"
    ],
    [
        "url",
        "image"
    ]
];

Your object doesn't have a length property or method-

you need to count its members.

var feedObjects={
    ["url","image"],["url","image"]
}
function count(){
    var counter= 0;
    for(var p in this){
        if(this.hasOwnProperty(p))++counter;
    }
    return counter;
}
count.call(feedObjects)

returned value: (Number)=2

or define an array:

var feedObjects=[ ["url","image"],["url","image"]];

//feedObjects.length=2;

Object.size = function(obj) {
     var size = 0, key;
     for (key in obj) {
         if (obj.hasOwnProperty(key)) size++;
     }
     return size;
 };
alert(Object.size(feedObjects))
发布评论

评论列表(0)

  1. 暂无评论