按照Mozilla docs内的对象解构,这里是如何使用解构一个for of
循环中:为...循环,并与阵列
var people = [
{
name: 'Mike Smith',
family: {
mother: 'Jane Smith',
father: 'Harry Smith',
sister: 'Samantha Smith'
},
age: 35
},
{
name: 'Tom Jones',
family: {
mother: 'Norah Jones',
father: 'Richard Jones',
brother: 'Howard Jones'
},
age: 25
}
];
for (var {name: n, family: {father: f}} of people) {
console.log('Name: ' + n + ', Father: ' + f);
}
// "Name: Mike Smith, Father: Harry Smith"
// "Name: Tom Jones, Father: Richard Jones"
我的问题是,会是什么正确的解构语法被万一family
物体位于一个阵列内,像这样:
var people = [
{
name: 'Tom Jones',
family: [
{
mother: 'Norah Jones',
father: 'Richard Jones',
brother: 'Howard Jones'
}
],
age: 25
}
];
(注额外组[方括号]的)
试图使用以解构:
for (var {name: n, family[0]: {father: f}} of people) {
console.log('Name: ' + n + ', Father: ' + f);
}
给出在方括号的Unexpected token
错误。
那么在这个例子中,我该如何使用解构来赋值给f
?
这很好用!谢谢。现在不需要其余的语法,但将它归档以备将来参考 –
是的,不客气。 – llama