数组解构+扩展语法仅具有特定字段?

时间:2018-08-17 07:16:59

标签: javascript arrays destructuring spread-syntax

let arr = [{name: 'john', age:17}, {name: 'Doe', age: 21}];

//let dropDownHumanOptions = ['All', ...arr.name, 'Cancel']
//let dropDownHumanOptions = ['All', ...{arr.name}, 'Cancel']

//Expected: let dropDownHumanOptions = ['All', 'john', 'Doe', 'Cancel']

我想知道是否存在任何类似的语法,我们可以使用扩展语法提取一个字段并合并到另一个数组的中间?

1 个答案:

答案 0 :(得分:4)

您需要散布映射的名称。

var array = [{name: 'john', age:17}, {name: 'Doe', age: 21}],
    dropDownHumanOptions = ['All', ...array.map(({ name }) => name), 'Cancel'];

console.log(dropDownHumanOptions);