0

Hey guys I need soe help please :P

What's wrong with this code?

var arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
var narr = []; // want to be like that [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

function arange(mass) {
    for (var i = 0; i < mass.length; i++) {
        for (var j = 0; j < mass[i].length; j++) {
            narr[mass[i]].push(mass[j][i]);
        }
    }
}

If i call the function there is a problem : "TypeError: Cannot read property 'push' of undefined." Thank you. Also link here

2
  • The error is telling you what is wrong. narr[mass[i]] is undefined. Commented Apr 21, 2016 at 19:07
  • narr.push(mass[j][i]); will give [1,4,7,2,5,8,3,6,9] so you'll need to think about how to create another level of array within the array to encapsulate each inner set Commented Apr 21, 2016 at 19:11

4 Answers 4

1

You accidentally tried to call push on a value of narr instead on the array itself. Of course a primitive value has no push function defined. Your code should look like this:

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

function arange(mass) {
    for (var i = 0; i < mass.length; i++) {
        var innerArray = [];
        for (var j = 0; j < mass[i].length; j++) {
            innerArray.push(mass[j][i]);
        }
        narr.push(innerArray);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

output need to be like that [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
1

Just take the right elememt to assign the value.

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

function arange(mass) {
    var narr = [];
    for (var i = 0; i < mass.length; i++) {
        for (var j = 0; j < mass[i].length; j++) {
            narr[j] = narr[j] || [];
            narr[j][i] = mass[i][j];
        }
    }
    return narr;
}

document.write('<pre>' + JSON.stringify(arange(arr), 0, 4) + '</pre>');

ES6

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

function arange(mass) {
    var narr = [];
    mass.forEach((a, i) => a.forEach((b, j) => {
        narr[j] = narr[j] || [];
        narr[j][i] = mass[i][j];
    }));
    return narr;
}

document.write('<pre>' + JSON.stringify(arange(arr), 0, 4) + '</pre>');

Comments

0

Or you could use this ES6 one liner

var arr = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ];
    
var res = arr.map((e, i) => e.map((_, j) => arr[j][i]));

document.write(JSON.stringify(res));

Comments

0

You need to add a temporary array for storing intermediate values:

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

var narr = []; 

function arange(mass) {
    for (var i = 0; i < mass.length; i++) {
        var t = []; // temporary array
        for (var j = 0; j < mass[i].length; j++) {
            t.push(mass[j][i]);
        }
        narr.push(t);
    }
}

arange(arr);

document.write(JSON.stringify(narr));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.