How to get the indexes of a json with javascript?

Asked

Viewed 837 times

1

For example:

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

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

Something like:

function(json){
       //faz o processo
      return indices;
}
  • Possible duplicity! The following issues should solve the proposed problem. http://answall.com/questions/17765/como-pega-o-%C3%adndice-de-um-objeto-javascript-buscando-pelo-valor? Rq=1 http://answall.com/questions/86839/howto filter_data-de-um-json-com-js?rq=1

1 answer

5


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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.