I have this array structure:
我有这个数组结构:
mdarray = {
'0001':{address:'add1',title:'title1'},
'0002':{address:'add2',title:'title2'},
'0003':{address:'add3',title:'title3'}
};
I wish to only work with the array if it has one or more items in it. Usually with an array I would use if (mdarray.length > 0) {}
but when I do this with the array above, mdarray.length
returns 'undefined'.
我希望只有数组中有一个或多个项目才能使用它。通常使用数组我将使用if(mdarray.length> 0){}但是当我使用上面的数组执行此操作时,mdarray.length返回'undefined'。
Is this is because it is an array of arrays? Is there another way to very simply pull back the number if items in the root of the array?
这是因为它是一个数组数组吗?是否有另一种方法可以简单地拉回数组根目录中的数字?
Or it is because the keys are strings and not integers?
或者是因为键是字符串而不是整数?
I've played about with different array structures and read about multidimensional arrays but I've not yet found an answer.
我玩过不同的数组结构并阅读多维数组,但我还没有找到答案。
2 个解决方案
#1
2
Modified code: you are creating Object instead of Array you should use following code: see this thrad
修改后的代码:您正在创建Object而不是Array,您应该使用以下代码:请参阅此thrad
mdarray = [
{address:'add1',title:'title1'},
{address:'add2',title:'title2'},
{address:'add3',title:'title3'}
];
mdarray.length // 3
mdarray.length // 3
#2
2
That is an Object, not an Array. So it has no length!
这是一个Object,而不是一个Array。所以没有长度!
#1
2
Modified code: you are creating Object instead of Array you should use following code: see this thrad
修改后的代码:您正在创建Object而不是Array,您应该使用以下代码:请参阅此thrad
mdarray = [
{address:'add1',title:'title1'},
{address:'add2',title:'title2'},
{address:'add3',title:'title3'}
];
mdarray.length // 3
mdarray.length // 3
#2
2
That is an Object, not an Array. So it has no length!
这是一个Object,而不是一个Array。所以没有长度!