admin管理员组

文章数量:1325236

I'm trying to loop through an array of values contained within an object which is itself contained in another object.

The object looks like this:

var guitar = {
  high4high5: {
    name: 'high 4th, high 5th',
    tuning: [ [5,4], [-2,3], [2,3], [7,3] ]
  },
  high4low5: {
    name: 'high 4th, low 5th',
    tuning: [ [5,4], [-2,3], [2,3], [7,2] ]
  }
}

I know I can just keep looping with a jQuery each loop like so:

$.each(guitar, function(key, value) {
  console.log('1st loop: ' + key, value);
  $.each(value, function(key, value) {
    console.log('2nd : ' + key, value);
    $.each(value, function(key, value) {
      console.log('3rd : ' + key, value);
    });
  });
});

But obviously this end up looping through everything again and again.

The data I need to get is the 'name' (string) and the 'tuning' (array) of each object.

I assume there's a better way to get what I want than just endless loops!

Probably important to note is that I won't know the name of the object inside the object ('high4high5' etc), but I WILL know that values within this object will always be name: (string) and tuning: (array).

EDIT:

Ok, I figured it out.

$.each(guitar, function(key, value) {
  var tuningName = value.name;
  var tuningArray = value.tuning;
  console.log('name: ' + tuningName);
  $.each(tuningArray, function(key,value) {
    console.log(value);
  });
});

Phew!

I'm trying to loop through an array of values contained within an object which is itself contained in another object.

The object looks like this:

var guitar = {
  high4high5: {
    name: 'high 4th, high 5th',
    tuning: [ [5,4], [-2,3], [2,3], [7,3] ]
  },
  high4low5: {
    name: 'high 4th, low 5th',
    tuning: [ [5,4], [-2,3], [2,3], [7,2] ]
  }
}

I know I can just keep looping with a jQuery each loop like so:

$.each(guitar, function(key, value) {
  console.log('1st loop: ' + key, value);
  $.each(value, function(key, value) {
    console.log('2nd : ' + key, value);
    $.each(value, function(key, value) {
      console.log('3rd : ' + key, value);
    });
  });
});

But obviously this end up looping through everything again and again.

The data I need to get is the 'name' (string) and the 'tuning' (array) of each object.

I assume there's a better way to get what I want than just endless loops!

Probably important to note is that I won't know the name of the object inside the object ('high4high5' etc), but I WILL know that values within this object will always be name: (string) and tuning: (array).

EDIT:

Ok, I figured it out.

$.each(guitar, function(key, value) {
  var tuningName = value.name;
  var tuningArray = value.tuning;
  console.log('name: ' + tuningName);
  $.each(tuningArray, function(key,value) {
    console.log(value);
  });
});

Phew!

Share Improve this question edited Aug 29, 2011 at 8:48 Richard Sweeney asked Aug 29, 2011 at 8:38 Richard SweeneyRichard Sweeney 7821 gold badge14 silver badges26 bronze badges 1
  • If you figured it out, you should post it as an answer rather than editing it into the question. You can earn rep that way, and people are more likely to post other answers if they think that their approach is better. – karim79 Commented Aug 29, 2011 at 8:51
Add a ment  | 

2 Answers 2

Reset to default 7

You really don't need jQuery to do this. You can do it with good old fashioned Javascript:

for(g in guitar) {
    console.log(guitar[g].name);
    console.log(guitar[g].tuning);
}

I'll say here that I've managed to answer my own question using a jQuery each loop. It's definitely a more verbose way to do it!

$.each(guitar, function(key, value) {
  var tuningName = value.name;
  var tuningArray = value.tuning;
  console.log('name: ' + tuningName);
  $.each(tuningArray, function(key,value) {
    console.log(value);
  });
});

本文标签: javascriptloop through array within objectwithin another object with jQueryStack Overflow