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!