1

I get data in console in this form

0:(8) [70, 61, 74, 79, 76, 63, 51, 51]
1:(4) [64, 84, 84, 66]
2:(2) [79, 69]
3:(3) [77, 100, 98]

I would like to print my data in console like this:

0:(4) [70, 64, 79,  77]
1:(4) [61, 84, 69, 100]
2:(4) [74, 84,  0,   0]
3:(4) [79, 66, 98,   0]
4:(4) [76,  0,  0,   0]
5:(4) [63,  0,  0,   0]
6:(4) [51,  0,  0,   0]
7:(4) [51,  0,  0,   0]

With this two function I get max of number in row and I fill empty column with 0, but I would like to show data like in example that I write.

function fillWithZeros(arr, length) {
for (var i = 0; i < length; i++) {
    if (arr[i] == null) {
        arr[i] = 0;
    }
}

return arr;
};
function findMaxEvents(item){
var maxNumEvents = 0;
for (var i = 0; i < item.length; i++){
    if (item[i] > maxNumEvents){
        maxNumEvents = item[i];
    }
}

return maxNumEvents;
}
mrkibzk
  • 227
  • 4
  • 17

3 Answers3

1

I hope you are actually looking for Transposing the existing array. Thanks to Transposing a 2D-array in JavaScript..

The obtained answer is a bit different from yours. Hope this helps to try and find the right way to reach your goal.

var array = [[70, 61, 74, 79, 76, 63, 51, 51], 
[64, 84, 84, 66], 
[79, 69], 
[77, 100, 98]];

var arrayWithMaxlen = array.map(function(a){return a.length;}).indexOf(Math.max.apply(Math, array.map(function(a){return a.length;})));
console.log("arrayWithMaxlen : " +arrayWithMaxlen); //gets index of array with max length

var newArray = array[arrayWithMaxlen].map(function(col, i) { 
  return array.map(function(row) { 
    if(row[i]!=undefined)return row[i];
    else return 0;
  })
});

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

The operation you're describing is basically a simple matrix transpose. There are many ways to do that. Here's one alternative:

var array = [
  [70, 61, 74, 79, 76, 63, 51, 51],
  [64, 84, 84, 66],
  [79, 69],
  [77, 100, 98]
];

function transpose(arr){
  var largest = arr.reduce(function(max, i){ return max.length > i.length ? max : i; }, []);
  return largest.map(function(col, i){
    return arr.map(function(row){
      return row[i] || 0;
    });
  });
}

console.log(transpose(array));
Nikolaj Dam Larsen
  • 5,455
  • 4
  • 32
  • 45
0

Just transpose it.

BTW, the accepted answer does rely on the first inner array length. If it is shorter then the others all greater indices are gone.

var array = [[70, 61, 74, 79, 76, 63, 51, 51], [64, 84, 84, 66], [79, 69], [77, 100, 98]],
    result = array.reduce(function (r, a, i) {
        a.forEach(function (b, j) {
            r[j] = r[j] || Array.apply(null, { length: array.length }).map(function () { return 0; });
            r[j][i] = b;
        });
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 1
    Though I agree with the implementation, there is either a mistake in expected output or some weird reason, but `3:(4) [79, 66, 98, 0]` makes your output incorrect and might attract unwanted comments. – Rajesh Oct 13 '17 at 09:46