简体   繁体   中英

Array Destructuring + spread syntax with specific field only?

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']

I'm wondering if there is any similar syntax where we can just extracting one of the field and combine in the middle of another array by using spread syntax?

You need to spread the mapped names.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM