How do I get indexes of a json with javascript?

1

For example:

var carro = {
    "modelo": "celta",
    "ano": 2007
}

How can I do a generic function that returns the index names of this json, in this case "model" and "year"?

Something like:

function(json){
       //faz o processo
      return indices;
}
    
asked by anonymous 16.03.2017 / 08:57

1 answer

5

var carro = {
    "modelo": "celta",
    "ano": 2007
}
var chaves = Object.keys(carro);
alert(JSON.stringify(chaves));

There is Object.keys(meuObjeto); that does just that.

    
16.03.2017 / 09:08