Javascript-将已破坏的项放入新变量中
我试图对一个变量使用析构函数。使用MDN中的示例:Javascript-将已破坏的项放入新变量中,javascript,Javascript,我试图对一个变量使用析构函数。使用MDN中的示例: var people = [ { name: 'Mike Smith', family: { mother: 'Jane Smith', father: 'Harry Smith', sister: 'Samantha Smith' }, age: 35 }, {
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"
以上内容将在一个循环中拆分出两行。我希望for in循环产生的信息被放回一个新变量中,以便我可以从服务器(使用Express.js)将其发送到客户端。您可以尝试以下操作:
let results = [];
for (var {name: n, family: {father: f}} of people) {
const result = 'Name: ' + n + ', Father: ' + f;
console.log(result);
results.push(result);
//Put results into a variable here
}
console.log('results', results);
现在,您可以使用这个结果数组执行任何操作 您可以尝试以下操作:
let results = [];
for (var {name: n, family: {father: f}} of people) {
const result = 'Name: ' + n + ', Father: ' + f;
console.log(result);
results.push(result);
//Put results into a variable here
}
console.log('results', results);
现在,您可以使用这个结果数组执行任何操作 如果我正确理解了您的问题,您只需将其放入数组(或其他任何内容):
如果我正确理解了您的问题,您只需将其放入一个数组(或其他任何数组):
。。。那就这么做吧。你有问题吗?请检查并重试。此处与销毁var无关。这个问题的标题是错误的。只需将新结构的数据放入一个数组中,并使用AJAX或其他方法将其发送回服务器。。。那就这么做吧。你有问题吗?请检查并重试。此处与销毁var无关。这个问题的标题是错误的。只需将新构造的数据放入一个数组中,然后使用AJAX或其他方法将其发送回服务器