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.

2
  • 7
    no this is because this is not an array at all. This is called an object literal Commented Oct 16, 2012 at 14:44
  • 1
    That isn't an array structure, that is an Object structure, and objects don't have length. Commented Oct 16, 2012 at 14:45

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Note that it is not a multi-dimensional array. It is an array of objects.
@Sushil Ah ok thanks. I think I understand the differences now, thanks for the thread link also.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.