1

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'.

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.

Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
Mere Development
  • 2,439
  • 5
  • 32
  • 63

2 Answers2

2

That is an Object, not an Array. So it has no length!

Hans Wassink
  • 2,529
  • 3
  • 30
  • 45
2

Modified code: you are creating Object instead of Array you should use following code: see this thrad

mdarray = [
  {address:'add1',title:'title1'},
  {address:'add2',title:'title2'},
  {address:'add3',title:'title3'}
];

mdarray.length // 3

Community
  • 1
  • 1
Anoop
  • 23,044
  • 10
  • 62
  • 76