Count items in JSON

1

What would be the way to count the items of a json response with this structure:

[['dato', 'dato', 'dato'], ['dato', 'dato', 'dato']]

and so on.

I know that using this function count = Object.keys(res).length; you can count the items of each element, I want to count all the items of all the elements.

Thanks for your help.

    
asked by Santiago D'Antuoni 07.12.2016 в 20:58
source

2 answers

4

That structure is an array of arrangements; an option would be to flatten that array using [].concat.apply([], elArray) and then get its length.

With flattening, I mean converting (temporarily and for calculation) this structure:

[['dato', 'dato', 'dato'], ['dato', 'dato', 'dato']]

in this other:

['dato', 'dato', 'dato', 'dato', 'dato', 'dato']

Salu2, an example:

var datos = [['dato', 'dato', 'dato'], ['dato', 'dato', 'dato']]
  
console.log(
    [].concat.apply([], datos).length
)
    
answered by 07.12.2016 / 21:22
source
0
var data = /* Su JSON sintetizado aquí */
var numberOfElements = data.res.length;

var o = JSON.parse(res);
alert(o.features.length);
    
answered by 07.12.2016 в 21:19