0

How do I console.log a list of the keys of an object (e.g. "12342341" in the code below)?

var data =
  {
    "12342341": {
      "City": "Montgomery",
      "State": "AL"
    },
    "23482342": {
      "City": "New York",
      "State": "NY"
    }
  }

console.log('City: ', data[12342341].City);
Makyen
  • 31,849
  • 12
  • 86
  • 121
user3731460
  • 334
  • 4
  • 14
  • 2
    `console.log('City: ', data['12342341'].City);` – Rayon Aug 03 '16 at 20:04
  • @Rayon That's logging the city, but it seems OP wants to list the array *names* (eg 12342341) , not their content. – Tyler Roper Aug 03 '16 at 20:05
  • @TylerRoper – The code OP has provided will return nothing... – Rayon Aug 03 '16 at 20:05
  • I'm not refuting that, simply clarifying that OP's question was "How do I console.log a list of the array names (e.g. 12342341)?". Being that your comment included no context, it could be misconstrued as an answer to his question, though it is not. – Tyler Roper Aug 03 '16 at 20:07
  • 2
    Terminology note: the code shown is neither JSON nor an array. It's an object. – nnnnnn Aug 03 '16 at 20:08
  • `Object.keys(data).map(k => data[k].City)` I downvote because I feel that there is nothing idiomatic. You just extract the data and print them. It is stupid to ask how to print the data when you are aware of the console.log. Similarly, there is nothing idiomatic about extracting a field called name`. Will you create another question to ask how to extract the states? – Little Alien Aug 03 '16 at 20:10
  • @LittleAlien - The OP is asking how to extract the names of properties, not how to use `console.log()`. They don't know how to extract "12342341" and "23482342" (they must not know about `Object.keys()`). – nnnnnn Aug 03 '16 at 20:13
  • @LittleAlien – Well in that case, you may down-vote the question.. Why answer ? – Rayon Aug 03 '16 at 20:14
  • 1
    @Rayon : `data[12342341].City` would have the same effect... since property accessor convert it to string internally https://jsfiddle.net/pranavcbalan/8zn29amd/ – Pranav C Balan Aug 03 '16 at 20:15
  • 1
    @user3731460 - I have (again) removed the "json" tag from your question because there is no JSON in your question. JSON is a *string* format used for data interchange. What you have is simply an object. – nnnnnn Aug 03 '16 at 20:18
  • 1
    @nnnnnn I have removed "JSON array" from the title, as there is no JSON in this question. – Blue Aug 03 '16 at 20:19

1 Answers1

3

Use Object.keys() methods to retrieve the property name array and Array#map method for generating City property value array from it.

var data = {
  "12342341": {
    "City": "Montgomery",
    "State": "AL"
  },
  "23482342": {
    "City": "New York",
    "State": "NY"
  }
};

// get property name array
var arr = Object.keys(data);

// iterate over property name and
// generate `City` property value array
var names = arr.map(function(v) {
  return data[v].City
});

console.log(arr, names);

FYI : If you just want the property name array then avoid the Array#map part.

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188