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']
我想知道是否存在任何类似的语法,我们可以使用扩展语法提取一个字段并合并到另一个数组的中间?
答案 0 :(得分:4)
您需要散布映射的名称。
var array = [{name: 'john', age:17}, {name: 'Doe', age: 21}],
dropDownHumanOptions = ['All', ...array.map(({ name }) => name), 'Cancel'];
console.log(dropDownHumanOptions);