2

Hi i am having an array like this, I want to make each Element as an array, i used chunk in lodash but no use. The

[ 'Product 1', 'Product 2', 'Product 3', 'Product 4', 'Product 5', 'Product 6' ]

Result i am expecting is like this

[[ 'Product 1'],['Product 2'], ['Product 3'], ['Product 4'], ['Product 5'],['Product 6' ]]

can anybody Help Me on this

4 Answers 4

3

if it is a onedimensional array, you can use the map function:

var arr = ['Product 1', 'Product 2', 'Product 3', 'Product 4', 'Product 5', 'Product 6'];


var result = arr.map(function(el) {
  return [el];
});

console.log(result);

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

1 Comment

Good use of a native function over a 3rd party library.
1

You can refer this also -

var arr1 = ['Product1', 'Product2', 'Product3', 'Product4'];

var arr2 = [];

for(var i=0;i<arr1.length;i++){
   var temparr = [];
   temparr.push(arr1[i]);
   arr2.push(temparr);
}
console.log(arr2);

Fiddle - https://jsfiddle.net/sajalsuraj/89vLty7k/

Comments

0

Check this out..

 var x = [ 'Product 1', 'Product 2', 'Product 3', 'Product 4', 'Product 5', 'Product 6' ]
      var y = [];
      for(var i = 0;i<x.length;i++){
        var temp = [x[i]];
        y.push(temp);
      }
      console.log(y);

Comments

0

If you are using it in Node.js or is ES6 environment you could do:

let arr = products.map(p => [p]);

Example: http://www.es6fiddle.net/ips7zgh0/

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.