Joe
Joe

Reputation: 546

How to access multi level object data with jQuery

I have this code in js, on click this happens:

  var self = $(this);
  self.click(function(e){
    e.preventDefault();

    var nid = self.parents('.innerContainer').attr('nid');
    var subjectTitleNID =  settings.xxxxx.yyyy["nid-" + nid]

Via HTML I can find the NID value of InnerContainer, which is the main parent.
From the console, if I run Drupal.settings.xxxx.yyyyy (where xxxx and yyyy are my destinations), I get a list of objects which are children.
["nid-463"]
["nid-465"]
["nid-466"] etc ....

nid-466 is the value assigned to VAR NID.

But what I need to find now, is:
1. How many children there are in ["nid-466"]
2. What are their values

Usually I would run a simple for loop, but I don't know how to target those values.

For example, I would do this:

for (i=0; i < dont know what to put here .length; i++) {
    > Drupal.settings.xxxx.yyyy[nid-466][nid-??]  // this is incorrect
    }

See image for more detailed structure.

Object Structure

Any ideas? Thanks George

Upvotes: 0

Views: 159

Answers (1)

Yurii Kovalenko
Yurii Kovalenko

Reputation: 419

Use $.each loor for this:

$.each(Drupal.settings.xxxx.yyyy[nid-466], function(index, value) {
    // index is a key 
    // value is a object
    // put your code here 
    // console.log(value.nid);
})

Upvotes: 1

Related Questions