如何遍历JavaScript对象中的所有成员,包括作为对象的值.
例如,我怎么能循环这个(访问每个的"your_name"和"your_message")?
var validation_messages = {
"key_1": {
"your_name": "jimmy",
"your_msg": "hello world"
},
"key_2": {
"your_name": "billy",
"your_msg": "foo equals bar"
}
}
Run Code Online (Sandbox Code Playgroud) 有没有办法(在jQuery或JavaScript中)循环遍历每个对象,它的子孙和孙子等等?
如果是这样......我还可以读他们的名字吗?
例:
foo :{
bar:'',
child:{
grand:{
greatgrand: {
//and so on
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以循环应该做这样的事情......
loop start
if(nameof == 'child'){
//do something
}
if(nameof == 'bar'){
//do something
}
if(nameof =='grand'){
//do something
}
loop end
Run Code Online (Sandbox Code Playgroud) 我正在尝试遍历嵌套对象以检索由字符串标识的特定对象.在下面的示例对象中,标识符字符串是"label"属性.我无法绕过如何遍历树以返回适当的对象.任何帮助或建议将不胜感激.
var cars = {
label: 'Autos',
subs: [
{
label: 'SUVs',
subs: []
},
{
label: 'Trucks',
subs: [
{
label: '2 Wheel Drive',
subs: []
},
{
label: '4 Wheel Drive',
subs: [
{
label: 'Ford',
subs: []
},
{
label: 'Chevrolet',
subs: []
}
]
}
]
},
{
label: 'Sedan',
subs: []
}
]
}
Run Code Online (Sandbox Code Playgroud)