1

I am new to programming and I have just worked out a multidimensional array problem by using pure javascript but I think I made it too complicated and just wondering can anyone tell me how do you think about my method and do you have a better way to do it. the problem is to console.log all the numbers in the arrays and the array is [[[1,2],[3,4]],[[5,6]]]. My code is as below, please post your suggestions,thanks

     var nestedArr = [[[1,2],[3,4]],[[5,6]]];
     var nestedArrOne = nestedArr[0];
     var nestedArrTwo = nestedArr[1];
     var newArr = nestedArrOne.concat(nestedArrTwo);
      function showAll(){
        for(var i=0; i<newArr.length; i++){
        for(var j=0; j<newArr[i].length; j++){
        console.log(newArr[i][j]);
                }
            }
       }

       showAll();
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sen123
  • 97
  • 1
  • 8
  • This is called "flatten", not "sort" around these parts ;) Look it up. – georg Jun 08 '17 at 08:57
  • Hi @Sen123 I believe, you should post the question in [LINK](https://codereview.stackexchange.com/) – Sudipta Mondal Jun 08 '17 at 08:57
  • You have a couple of issues with your code, but here's one to get started with: When you pass an index to an array, you get the next item in the array, not the next nested level. So `nestedArr` only has one item in it `[[1,2],[3,4],[5,6]]` which is `nestedArr[0]` - `nestedArr[1]` isn't anything. – Brett East Jun 08 '17 at 08:59
  • btw, you don't sort something. – Nina Scholz Jun 08 '17 at 09:01
  • See also https://stackoverflow.com/a/44103808/1647737 – le_m Jun 08 '17 at 10:05

3 Answers3

0

You could iterate the nested array and check if the item is an array, then call the function again for the inner array.

function iter(array) {
    var i;                               // declare index
    for (i = 0; i < array.length; i++) { // iterate array
        if (Array.isArray(array[i])) {   // check if item is an array
            iter(array[i]);              // if so, call iter with item
            continue;                    // and continue the loop
        }
        console.log(array[i]);           // the wanted output
    }
}

var nestedArr = [[[1, 2], [3, 4]], [[5, 6]]];
iter(nestedArr);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

The recursive function could be used to flatten the array.

let isArray = val => val.constructor === Array

let log = val => console.log(val)

let flatten = val =>
  val
    .reduce((acc, cur) =>
      isArray(cur) ? [...acc, ...flatten(cur)] : [...acc, cur],
      [])


// Running examples
let arrA = [[[1,2],[3,4]],[[5,6]]]
flatten(arrA).forEach(log)

log('-------------------')

let arrB = [[[1,2],[3,4]],[[5,6]],[7],[[[[9]]]]]
flatten(arrB).forEach(log)
shuaibird
  • 1,543
  • 1
  • 11
  • 5
0

A fancy Haskellesque approach with "pattern matching by rest operator" could be.

var flat = ([x,...xs]) => x ? [...Array.isArray(x) ? flat(x) : [x], ...flat(xs)] : [];
    na   = [[1,2],[3,[4,5]],[6,7,[[[8],9]]],10];
    fa   = flat(na);
console.log(...fa);
Redu
  • 25,060
  • 6
  • 56
  • 76