1
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?

1
  • 1
    You can only destructure to a standalone variable. For what you're looking for, you need to .map Commented Aug 17, 2018 at 7:18

1 Answer 1

4

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.