-1

could you please tell me how to convert an array which is

["apple","banana", "mango"]

to

[{fruit:"apple"},{fruit:"banana"},{fruit:"mango}]

with the parenthesis

something similar to this but with parenthesis JavaScript Add key to each value in array

pram
  • 5
  • 2

1 Answers1

2

You're still looking for the .map() function, just like that other question. The only difference is that you want to return an object from the callback, not just a string.

For example:

let input = ["apple","banana", "mango"];
console.log(input.map(x => ({fruit: x})));

As an aside, {} are not parentheses, they are curly braces.

David
  • 208,112
  • 36
  • 198
  • 279
  • Why does this deserve its own new answer? And why is this not the nth dupe of one of the _"convert array of strings into array of objects"_ questions? – Andreas Nov 10 '21 at 16:08