-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

0

1 Answer 1

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.

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

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.