0

I have a belwo collection of objects assigned in

  products:{id: null, name: 6801},
     {id: null, name: 6802},
     {id: null, name: 6805}

I need to use map function and iterate the name need to convert into array of strings.

current code is written in 0 : {names: 6197} 1 : {names: 6801} 2 : {names: 6802}

but i need the below format

names:[6802,6802,6805],
    products.map(({name: names}) => ({names}));
4
  • 1
    Such as: let names = products.map(p => p.name); Commented Sep 10, 2018 at 15:53
  • @tymeJv which is assigned to names Commented Sep 10, 2018 at 15:55
  • 2
    Assuming you mean "destructuring" instead of "destruction", is there some reason you are doing it that way instead of p => p.name? If you need destructuring you would do ({name: n} => n). What is the point of this question? Commented Sep 10, 2018 at 15:58
  • which is not return in array of string [6802,6803] and return in 0 : {name: 6197} 1 : {domicile: 6801} 2 : {name: 6802} Commented Sep 10, 2018 at 16:00

2 Answers 2

1

You seem to be looking for

products.map(({name}) => name);

Don't put the result value in braces, that would form another object literal.

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

Comments

1

try

let productNamesArray = products.map(ele=>{return ele.name;});

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.