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
4 Answers
Reset to default 8You 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))