Home > Blockchain >  How to create nested array of different length from a multidimensional array
How to create nested array of different length from a multidimensional array

Time:06-24

I have an array that looks like this:

arr = [[1,2,3],
       [4,5,6],
       [7,8,9]]

I have initialized an empty array and I want put the diagonals of the arr inside the new array. So i've tried this:

arr = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]
new_arr = [];
tail = arr.length - 1;
for (let i = 0; i < arr.length; i  ) {
  for (let j = 0; j < arr[i].length; j  ) {
    if (j == i) {
      new_arr.push(arr[i][j]);
    }
    if (j == tail) {
      new_arr.push(arr[i][j]);
      tail--;
    }
  }
}

console.log(new_arr)

The logic seems to work but I can't seem to get the structure right. What I want is to nest two arrays inside the new array like this:

[[1,5,9],[3,5,7]]

But what I get is one array with the right values unordered. How to get the expected output? Any help is appreciated. Thanks!

CodePudding user response:

var arr = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];


var diagonal1 = [];
var diagonal2 = [];

for (var i = 0; i < arr.length; i  ) {
  diagonal1.push(arr[i][i]);
  diagonal2.push(arr[i][arr.length - i - 1]);
}

var new_arr = [diagonal1, diagonal2];
console.log(new_arr)

CodePudding user response:

The following solution would work for you if the width and height of the number matrix is always equal.


const arr = [[1,2,3],
       [4,5,6],
       [7,8,9]];

const result = [[],[]];
arr.map((row,index) => {
  result[0].push(row[0 index]);
  result[1].push(row[row.length - index - 1]);
});


console.log(result); // [[1,5,9],[3,5,7]]

CodePudding user response:

You need to have 2 separate temporary arrays. And you don't need nested loops. You can optimize the code like this with a single loop if you understand the math.

arr = [[1,2,3],
       [4,5,6],
       [7,8,9]];
       
function findDiagonals(arr) {
  const diagonal_1 = [];
  const diagonal_2 = [];
  
  for( let i = 0; i < arr.length; i   ) {
    diagonal_1.push(arr[i][i]);
    diagonal_2.push(arr[i][arr.length - (i 1)]);
  }
  
  return [diagonal_1, diagonal_2];
}

console.log(findDiagonals(arr));

CodePudding user response:

This is a O(n) approach by using the function Array.prototype.reduce.

const arr = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];
       
const result = arr.reduce(([left, right], array, row, {length}) => {
  return [[...left, array[row]], [...right, array[length - row - 1]]];
}, [[],[]]);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

the difficulty is low:

const
  arr = [[1,2,3],[4,5,6],[7,8,9]]
, new_arr = [[],[]]
  ;
let p0 = 0, p1 = arr.length
for( let inArr of arr)
  {
  new_arr[0].push( inArr[p0  ] )
  new_arr[1].push( inArr[--p1] )
  }

console.log( JSON.stringify( new_arr ))

in a one Line code:

const
  arr = [[1,2,3],[4,5,6],[7,8,9]]
, new_arr = arr.reduce(([lft,rgt],A,i,{length:Sz})=>([[...lft,A[i]],[...rgt,A[Sz-i-1]]]),[[],[]])
  ;

console.log( JSON.stringify( new_arr ))  // [[1,5,9],[3,5,7]]
  • Related