1

For example i have a array like that

var myArray = [
[1,2,3],
[4,5,6],
[7,8,9,10],
[[11,12],[13,14,15]]];

    for (var i = 0;i < myArray.length;i++){
    for(var j = 0 ;j< myArray[i].length;j++){
        for(var k = 0;k< myArray[i][j].length;k++){
            console.log(myArray[i],[j][k]);

        }
    }
        
      
}

   

But output is only 11,12,13,14,15. And i wanna print all the values Could some one help to fix Thank you in advance

4
  • Does this answer your question? Deep flat multidimensional array in Javascript Commented Dec 8, 2021 at 17:21
  • Write a function (x) that takes an array and iterates over the elements of that array. In the function test every element and if its an array call x with that array.... Commented Dec 8, 2021 at 17:27
  • You have different levels of nesting in your array. [1,2,3] is in the "second level" whereas [11,12] and [13,14,15] are in the "third level". So for instance myArray[i][j].length will be undefined for i == 0 Commented Dec 8, 2021 at 17:28
  • That would be the perfect moment to use a recursive function, recalling itself each time one entry is an Array! Commented Dec 8, 2021 at 18:34

3 Answers 3

0

Use e.g. Array.prototype.flat(), .flat(2) flattens to the level of 2 arrays deep.

var myArray = [
[1,2,3],
[4,5,6],
[7,8,9,10],
[[11,12],[13,14,15]]];

console.log(myArray.flat(2));
   

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

4 Comments

Why that "magic number" 2?
The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
Doesn't really answer my question ;)
you can write Infinity if you're not sure how many levels you have. was not specified in the question though
0

You can use recursive function first pass it the whole array then loop it and check if the element you are accessing is array or digit if its array call the same function again and only pass it this element otherwise if its digit just print it

This works even when you dont know how many nested arrays you have otherwise use MWO answer

var myArray = [
[1,2,3],
[4,5,6],
[7,8,9,10],
[[11,12],[13,14,15]]];

function printArray(arr){
  for(var i = 0; i < arr.length; i++){
    if(Array.isArray(arr[i])){
      printArray(arr[i]);
    }else{
      console.log(arr[i]);
    }
  }
}

printArray(myArray);

Comments

0

This fucntion works for any depth of multi dimensional array. Using Recursion

var myArray = [1,2,33,44, [1,2,3], [[11,12],[13,14,15]]];

var myArray1 = [1,2,33,44, [[11,12],[13,14,15]]];


var deepFlattenArray = function (array){
    var result = []; 
    
    array.forEach(function (element) {
      if (Array.isArray(element)) {
          result = result.concat(deepFlattenArray(element)); 
      } else {
          result.push(element);
      }
    });
    
    return result;
};

console.log(deepFlattenArray(myArray));
 // output = [ 1,  2, 33, 44,  1, 2,  3, 11, 12, 13, 14, 15]

console.log(deepFlattenArray(myArray1));
// output = [1,  2, 33, 44, 11, 12, 13, 14, 15 ]

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.