0

I would like to get length of the array in this object:

obj = {'2012-08-08':[1,2,5,8]}

I understand we can get length like this:

obj['2012-08-08'].length

But if it is not known the name of the key how we get the length of the array?

Say I have a loop:

_.each(dates, function(date){
     //each date is like the above javascript object, how to  get the length of the array?

})
3
  • 8
    This is beginning to feel like a lost battle, but still: that is not a JSON object. Commented Jun 10, 2013 at 20:05
  • 4
    Using variable names is a BAD idea. Leave variables for data and make structure easily predictable. Something like {date:'2012-08-08', values:[1,2,5,8]} will do the trick better. Commented Jun 10, 2013 at 20:06
  • 1
    That looks like underscore.js to me, not jQuery. If you've actually aliased jQuery to _, then please switch the tag back. Commented Jun 10, 2013 at 20:17

1 Answer 1

2

You have a date parameter in your call to each. Just call date.length.

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

1 Comment

You can do a for loop or use each again.

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.