我试图在变量上使用Destructing。使用MDN中的示例:
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);
//Put results into a variable here
}
// "Name: Mike Smith, Father: Harry Smith"
// "Name: Tom Jones, Father: Richard Jones"
以上将在循环中分割出2行。我想要的是将for-in循环产生的信息放回到一个新变量中,以便我可以将它从服务器(使用Express.js)发送到客户端。
答案 0 :(得分:1)
您可以尝试以下操作:
\rowcolors
此时,您现在可以使用此结果数组执行任何操作。
答案 1 :(得分:1)
如果我正确理解你的问题,你只需将它推入一个数组(或其他):
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
}
];
var results = []
for (var {name: name, family: {father: father}} of people) {
results.push({ name, father })
}
console.log(JSON.stringify(results));
// => [{"name":"Mike Smith","father":"Harry Smith"},{"name":"Tom Jones","father":"Richard Jones"}]