eat-sleep-code
eat-sleep-code

Reputation: 4855

Getting child data in a JavaScript object

I have been banging my head against this all night.

I have a service that is returning data that looks like this: enter image description here

You will see there are Objects with a GUID, nested under a parent object. I need to loop through all the "GUID" objects and get the attributes (i.e., author, content, etc.).

The GUIDs are dynamic (I don't know what they are ahead of time). The attributes below it are known.

I am having trouble figuring out how to target it. I can't seem to successfully use a for or forEach loop on it.

I need to use native JavaScript (i.e. no jQuery for this one).

Upvotes: 0

Views: 122

Answers (7)

Max Xiong
Max Xiong

Reputation: 163

I believe that you can iterate through all indexes using the advanced for loop. a.b is the same as a["b"] in javascript.

Upvotes: 1

Sammy I.
Sammy I.

Reputation: 2735

You can iterate thru the object properties like this:

for(let key in Response){
  if(!Response.hasOwnProperty(key))
    continue;
  //key will be each GUID
  let yourObject = Response[key] //Each object in the list of objects
}

You can read about for...in loops here

Hope that helps!

Upvotes: 0

Manikant Gautam
Manikant Gautam

Reputation: 3591

Here is another way to iterate through json Object

 var obj = {a: 1, b: 2, c: {a: 1, b: 2}};
 function walk(obj) {
 for (var key in obj) {
if (obj.hasOwnProperty(key)) {
  var val = obj[key];
  console.log(val);

         walk(val);
         }
      }
    }
 walk(obj);

var obj = {a: 1, b: 2, c: {a: 1, b: 2}};
function walk(obj) {
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
      var val = obj[key];
      console.log(val);
      
      walk(val);
    }
  }
}
walk(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

hazardous
hazardous

Reputation: 10837

Here's a possible solution:

var keys = Object.keys(data);
var results = 
keys.map(
  function(key){
      var datum = data[key];
      // do whatever with the data.
      return {
        "author" : data["author"]
      }
  }
)
// now results is an array of {"author"} objects.

Upvotes: 2

sumit chauhan
sumit chauhan

Reputation: 1300

If you are using angular try angular.forEach loop to iterate over all GUID's, else you can use for each in javascript. see below code snippet.

var user ={
  '1': {
  "name":'abc',
  "age":26
  },
  '2': {
  "name":'def',
  "age":28
  }
};

for(var key in user) {
  console.log(user[key].name);
}

Upvotes: 1

brk
brk

Reputation: 50326

In order to loop through an object use for...in

Since you have not posted the code of object , here is a snippet with dummy object

var x = {
  'a-45-2455': {
    'author': 'Some Name'
  }
}
   for(var keys in x){
    alert(x[keys].author)


}

Upvotes: 1

Atul Sharma
Atul Sharma

Reputation: 10720

    var x = {
  'a-45-2455': {
    'author': 'Some Name'
  }
};
    var keys = Object.keys(x);
    keys.forEach(function(key,value){
       var author = x[key]['author'];
       console.log(author);
    });

You can access the data in this way. You can also create another array from the values and use that.

Upvotes: 1

Related Questions