user1830833
user1830833

Reputation: 181

Looping through nested json object

I have a badly designed JSON object which unfortunately I cannot change at this moment which contains a number of objects. Here is an example of what I am working with:

var land = [
    {"name":"city","value":"Los Angeles"},
    {"name":"state","value":"California"},
    {"name":"zip","value":"45434"},
    {"name":"country","value":"USA"}
];

Here is how I am looping through i:

$(document).ready(function(){
    $.each(land, function(key,value) {
      $.each(value, function(key,value) {
          console.log(key + ' : ' + value);
      })
    }); 
})

The result is as follows:

name : city
value : Los Angeles
name : state
value : California
name : zip
value : 45434
name : country
value : USA

My goal is to have a result like this:

city : Los Angeles
state : California
zip : 45434
country: USA

What am I missing here? How do I achieve my intended result? Thank you in advance :)

Upvotes: 7

Views: 11934

Answers (6)

Pranav C Balan
Pranav C Balan

Reputation: 115222

If property names are same in all object(name and value) then do it like.

$.each(land, function(key,value) {
  console.log(value.name + ' : ' + value.value);
}); 

Upvotes: 2

lyoko
lyoko

Reputation: 429

You can do it using ecmascript 5's forEach method:

land.forEach(function(entry){ 
      console.log(entry.name + " : " + entry.value) 
} );

or use jquery to support legacy web browsers:

$.each(land,function(index,entry) {
     console.log(entry.name + " : " + entry.value)
});

Upvotes: 6

Arun Shinde
Arun Shinde

Reputation: 1185

Don't iterate through objects. You need only single loop to achieve this.

 $(document).ready(function(){
                $.each(land, function(key,value) {
                      console.log(value.name + ' : ' + value.value);
                }); 
            })

Upvotes: 2

E.Serra
E.Serra

Reputation: 1574

var converter = function(object){
    return {city: object[0].value,
           state: object[1].value,
           zip: object[2].value,
           country: object[3].value
    }
}

converter(land)

Object { city: "Los Angeles", state: "California", zip: "45434", country: "USA" }

Upvotes: 0

Barmar
Barmar

Reputation: 780984

Don't loop through the subobject, just show its properties.

var land = [{
  "name": "city",
  "value": "Los Angeles"
}, {
  "name": "state",
  "value": "California"
}, {
  "name": "zip",
  "value": "45434"
}, {
  "name": "country",
  "value": "USA"
}];

$.each(land, function(index, object) {
    console.log(object.name, ": ", object.value);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Upvotes: 4

Nenad Vracar
Nenad Vracar

Reputation: 122047

You only need one forEach() loop to get this result.

var land = [{
  "name": "city",
  "value": "Los Angeles"
}, {
  "name": "state",
  "value": "California"
}, {
  "name": "zip",
  "value": "45434"
}, {
  "name": "country",
  "value": "USA"
}];

land.forEach(function(e) {
  console.log(e.name + ' : ' + e.value);
})

Upvotes: 2

Related Questions