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
2 Answers
Reset to default 7You 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
版权声明:本文标题:javascript - loop through array within object, within another object with jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742166669a2425951.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论