1

I am having this array.

const array = ["Pizza","Pasta","Choco"];

I want to do this:

const array = [["Pizza"],["Pasta"],["Choco"]];

How can I do this?

2
  • You can use .map() to map each element to an array Commented Jan 23, 2020 at 9:13
  • @NickParsons can u write it? Commented Jan 23, 2020 at 9:19

1 Answer 1

1

Using Array.prototype.map(), you can provide a function (str => [str]) which wraps each of your string food elements in an array. This will give you a new array, which contains all your elements nested within their own arrays.

See example below:

const array = ["Pizza","Pasta","Choco"];
const result = array.map(str => [str]);
console.log(result);

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.