0

I have following JSON structure :

"123456789": {
"id": "Some_Id",
"code": "Some_Code",
"name": "Some_Name",
},
"123456789": {
"id": "Some_Id",
"code": "Some_Code",
"name": "Some_Name",
},,
"123456789": {
"id": "Some_Id",
"code": "Some_Code",
"name": "Some_Name",
}
}

Now if I have to find out name for every object inside JSON, how can I iterate?

Gaurav
  • 133
  • 2
  • 14

3 Answers3

-1
Object.keys(theObject).forEach(function (key) {
  var item = theObject[key];
  console.log(item.name);
});
Rudi
  • 2,987
  • 1
  • 12
  • 18
  • While this code snippet may solve the question, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Box Box Box Box Jun 03 '16 at 11:33
  • I think it's pretty much self explanatory, but you are free to edit and provide what you think is missing, that would be a much greater value than simply downvoting code answers. – Rudi Jun 03 '16 at 12:38
  • (BTW I haven't downvoted, I came across this in LQP) I shouldn't edit as that would be clearly conflicting with the author's intent. This answer might also be useful to future viewers, but always an explanation can be provided. If you do not want to put an explanation, it's alright. I was just suggesting a way for your answer to be more better. – Box Box Box Box Jun 03 '16 at 12:57
-1

You can use simple jQuery

arr = $.parseJSON(arr);
$.each(msg, function(i, key){
alert(key.id);
});

It is just an example to iterate

Apoorv
  • 231
  • 1
  • 8
-1

First of all, an object can not have multiple keys of same names.

Use Object.keys to get the keys of the object and iterate over them using Array#foreach

var input = {
  "123456789": {
    "id": "Some_Id",
    "code": "Some_Code",
    "name": "Some_Name",
  },
  "123456789": {
    "id": "Some_Id",
    "code": "Some_Code",
    "name": "Some_Name",
  },
  "123456789": {
    "id": "Some_Id",
    "code": "Some_Code",
    "name": "Some_Name",
  }
};
console.log(Object.keys(input)) //Only one key
Object.keys(input).forEach(function(item) {
  console.log(input[item].name);
});
Rayon
  • 36,219
  • 4
  • 49
  • 76