0

i have a JSON data from AJAX response as below

    {
        "2015001":{"sname":"name1","01-07-2015":null,"02-07-2015":"0","03-07-2015":"0","04-07-2015":"0","05-07-2015":null,"06-07-2015":"0","07-07-2015":"0","08-07-2015":"0","09-07-2015":"0","10-07-2015":"0","11-07-2015":null,"12-07-2015":null,"13-07-2015":"0","14-07-2015":"1","15-07-2015":null,"16-07-2015":"2","17-07-2015":null,"18-07-2015":null,"19-07-2015":null,"20-07-2015":"2","21-07-2015":"0","22-07-2015":"0","23-07-2015":"0","24-07-2015":"1","25-07-2015":"1","26-07-2015":null,"27-07-2015":"2","28-07-2015":null,"29-07-2015":"2","30-07-2015":"2","31-07-2015":"2"},
        "2015002":{"sname":"name2","01-07-2015":null,"02-07-2015":"0","03-07-2015":"1","04-07-2015":"1","05-07-2015":null,"06-07-2015":"0","07-07-2015":"0","08-07-2015":"0","09-07-2015":"0","10-07-2015":"2","11-07-2015":null,"12-07-2015":null,"13-07-2015":"1","14-07-2015":"1","15-07-2015":null,"16-07-2015":"2","17-07-2015":null,"18-07-2015":null,"19-07-2015":null,"20-07-2015":"0","21-07-2015":"0","22-07-2015":"0","23-07-2015":"0","24-07-2015":"0","25-07-2015":"0","26-07-2015":null,"27-07-2015":"0","28-07-2015":null,"29-07-2015":"0","30-07-2015":"0","31-07-2015":"0"},
        "2015003":{"sname":"name3","01-07-2015":null,"02-07-2015":"2","03-07-2015":"2","04-07-2015":"2","05-07-2015":null,"06-07-2015":"2","07-07-2015":"2","08-07-2015":"0","09-07-2015":"2","10-07-2015":"2","11-07-2015":null,"12-07-2015":null,"13-07-2015":"2","14-07-2015":"0","15-07-2015":null,"16-07-2015":"2","17-07-2015":null,"18-07-2015":null,"19-07-2015":null,"20-07-2015":"2","21-07-2015":"2","22-07-2015":"0","23-07-2015":"2","24-07-2015":"2","25-07-2015":"2","26-07-2015":null,"27-07-2015":"2","28-07-2015":null,"29-07-2015":"2","30-07-2015":"2","31-07-2015":"2"},
        "2015004":{"sname":"name4","01-07-2015":null,"02-07-2015":"2","03-07-2015":"2","04-07-2015":"2","05-07-2015":null,"06-07-2015":"0","07-07-2015":"2","08-07-2015":"2","09-07-2015":"2","10-07-2015":"2","11-07-2015":null,"12-07-2015":null,"13-07-2015":"2","14-07-2015":"2","15-07-2015":null,"16-07-2015":"2","17-07-2015":null,"18-07-2015":null,"19-07-2015":null,"20-07-2015":"0","21-07-2015":"2","22-07-2015":"2","23-07-2015":"2","24-07-2015":"2","25-07-2015":"0","26-07-2015":null,"27-07-2015":"2","28-07-2015":null,"29-07-2015":"0","30-07-2015":"2","31-07-2015":"2"},
        "2015005":{"sname":"name5","01-07-2015":null,"02-07-2015":"2","03-07-2015":"2","04-07-2015":"0","05-07-2015":null,"06-07-2015":"2","07-07-2015":"2","08-07-2015":"2","09-07-2015":"2","10-07-2015":"2","11-07-2015":null,"12-07-2015":null,"13-07-2015":"2","14-07-2015":"2","15-07-2015":null,"16-07-2015":"2","17-07-2015":null,"18-07-2015":null,"19-07-2015":null,"20-07-2015":"0","21-07-2015":"2","22-07-2015":"2","23-07-2015":"2","24-07-2015":"2","25-07-2015":"2","26-07-2015":null,"27-07-2015":"2","28-07-2015":null,"29-07-2015":"2","30-07-2015":"2","31-07-2015":"2"},
        "2015006":{"sname":"name6","01-07-2015":null,"02-07-2015":"2","03-07-2015":"2","04-07-2015":"2","05-07-2015":null,"06-07-2015":"2","07-07-2015":"2","08-07-2015":"2","09-07-2015":"2","10-07-2015":"2","11-07-2015":null,"12-07-2015":null,"13-07-2015":"2","14-07-2015":"2","15-07-2015":null,"16-07-2015":"2","17-07-2015":null,"18-07-2015":null,"19-07-2015":null,"20-07-2015":"2","21-07-2015":"0","22-07-2015":"2","23-07-2015":"2","24-07-2015":"2","25-07-2015":"2","26-07-2015":null,"27-07-2015":"2","28-07-2015":null,"29-07-2015":"2","30-07-2015":"2","31-07-2015":"2"}
    }

each object has same number of objects. Here i want to count the number of objects in the first object(2015001). The key will change upon every request.

i tried

console.log(Object.keys(data[2015001]).length)

and i got what i need

but how can i do without key(2015001)

Kishore Reddy
  • 147
  • 10

2 Answers2

5

If you always want to know the number of keys of the first object, then you should use Object.keys(data[Object.keys(data)[0]]).length

Note that if the order is important to you, you should send the keys inside an array and not inside an object, as according to the specification, an object is an unordered set of name/value pairs.

uri2x
  • 3,162
  • 1
  • 11
  • 25
  • Don't force the use of an array, only use an array if you have a list of things without a key, if you have a key, use an object. you are confusing arrays with hashtables but in js, hashtables are objects anyway – DarkMukke Aug 28 '15 at 08:28
  • @DarkMukke the issue here is that it seems that he always wants to check the first date in his list, and I guess he's assuming it'll be in the same order he entered/sent it to the client. Since an object is unordered, the order he expects might change in the example above, unless an array is used (which always keeps the ordering). See this as an example of the possible headaches I'm referring to: http://stackoverflow.com/questions/3948206/json-order-mixed-up – uri2x Aug 28 '15 at 08:32
  • fair enough, you make a valid point, but this is not in the scope of what the OP asked. what if the data he gets is coming from somewhere external, then you haven't given him the best solution ? – DarkMukke Aug 28 '15 at 08:34
  • First I've given him the solution, then a heads-up to beware of the dragons of this practice :) – uri2x Aug 28 '15 at 08:36
  • Wow, Thaaaaaaaaaaaank you. – Kishore Reddy Aug 28 '15 at 08:37
  • @uri2x yeh, sorry about that, misread the whole thing, i got fixated on the forcing arrays on people – DarkMukke Aug 28 '15 at 08:37
  • @KishoreReddy if the answer works for you, accept it by clicking the tick – DarkMukke Aug 28 '15 at 08:38
  • All i need is the keys in the first object and use them inside of table – Kishore Reddy Aug 28 '15 at 08:43
1

You could get the first key and use it to access the object, probably the fastest way, but then again your information is a bit unclear, you are talking about objects and more objects but I am unsure when you talk about the outer object or when about the inner object.

for (var k in Object) {
    break
}
console.log(Object.keys(data[k]).length)
Community
  • 1
  • 1
DarkMukke
  • 2,469
  • 1
  • 23
  • 31