Counting pairs of objects in a JSON array

I had a problem getting parameters from json chain, json I got something like this

[{"aa":"bb","ccc":"ddd","eeee":"ffff","ggggg":"hhhhh","iiiiii":"jjjjjj","kkkkkkk":"lllllll"}] 

I am trying to count the number of pairs inside "{}", but I do not know how to do this. I tried json.length and json[0].length , the first gave me the value "1" and the second gave me undefined .

+4
source share
1 answer

Your json object is an array containing one object. Thus, the length is 1. An object in an array has several properties (key / value pairs).

Therefore, in most modern browsers (except IE), this will work for you:

 Object.keys(json[0]).length 

See here the answers to various methods for iterating / counting object properties in JavaScript:

+2
source

Source: https://habr.com/ru/post/1443841/


All Articles