0

Below is my array if items that I want to reduce it to a single list of arrays..

var input=[
    [
        2
    ],
    [
        3,
        13
    ],
    [
        4,
        14
    ],
    [
        5,
        15,
        25,
        35
    ]
]

var output=[
    2,
    3,
    13,
    4,
    14,
    5,
    15,
    25,
    35
]

My code:

  function reduceArray(item){
                  for(var i=0;i<item.length;i++){
                      return i;
                  }
              }
    var result=result.map((item)=>{
                   if(item.length>0){
                       return reduceArray(item);
                   }else{
                       return item;
                   }
               }) 

which produces the same result.Can anyone please figure out where I'm doing wrong or any other approach to achieve this..Thanks

forgottofly
  • 2,577
  • 9
  • 41
  • 82

5 Answers5

3
input.reduce(function(a, x) { return a.concat(x); });
// => [2, 3, 13, 4, 14, 5, 15, 25, 35]

reduce sets the accumulator to the first element (or a starting value if provided), then calls the function with the accumulator and each successive element. The function we provide is concatenation. If we say input is [a, b, c], then the above command will be equivalent to a.concat(b).concat(c). [concat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) produces a new array by smushing two or more arrays together.

EDIT: Actually, there is another possible answer:

Array.prototype.concat.apply(input[0], array.slice(1));
// => [2, 3, 13, 4, 14, 5, 15, 25, 35]

This directly calls concat with multiple arguments; if input is again [a, b, c], then this is equivalent to a.concat(b, c). apply calls a function with a given receiver and arguments; slice will give us just a part of the array, in this case everything starting from the first element (which we need to chop off since it needs to be the receiver of the concat call).

Amadan
  • 169,219
  • 18
  • 195
  • 256
  • I am quiet sure this is best answer among all other answers, but... A nice little explanation will help others understand what is happening.. :) – Rayon May 23 '16 at 07:10
2

One liner would be

input = [[2],[3,13],[4,14],[5,15,25,35]];
[].concat.apply([],input);
Jagdish Idhate
  • 6,302
  • 6
  • 27
  • 46
1

You can use lodash's flattenDeep()

_.flattenDeep([1, [2, [3, [4]], 5]]);
// → [1, 2, 3, 4, 5]
fhollste
  • 755
  • 6
  • 15
1

User concat.check this for more information http://www.w3schools.com/jsref/jsref_concat_array.asp

var input=[[2],[3,13],[4,14],[5,15,25,35]];
var output=[];
for(var i = 0; i < input.length; i++)
{
  output = output.concat(input[i]);
}
console.log(output);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Ankur Bhadania
  • 3,954
  • 1
  • 22
  • 34
1

use concat is the perfect way

The concat() method is used to join two or more arrays. This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.

var newArr = [];
for(var i = 0; i < input.length; i++)
{
    newArr = newArr.concat(input[i]);
}

console.log(newArr);
David Jaw Hpan
  • 4,423
  • 1
  • 22
  • 49