Reputation: 57
I wants to reverse the array with sub arrays with nested loops. I do not wants to use the reverse function. But every-time I do that I get this a type-error.
anyways here is my code
let arr = [[1,2,3],[4,5,6],[7,8,9]];
for(let i = arr.length - 1; i > 0; i--)
{
for (let j = arr[i].lenght - 1; j > 0; j--)
{
console.log(arr[i][j]);
}
}
The result should be 9,8,7,6,5,4,3,2,1 but instead I get nothing blank.
Upvotes: 1
Views: 860
Reputation: 1259
let arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
let result = arr
.reduce((prev, current) => {
return [...prev, ...current];
}, [])
.reduce((prev, current) => {
return [current, ...prev];
}, []);
console.log("result is", result);
You can use this also, using reduce let me know if it works for you or not
Upvotes: 2
Reputation: 1301
You had a typo in the word length
and also it should be >= 0
let arr = [[1,2,3],[4,5,6],[7,8,9]];
for(let i = arr.length - 1; i >= 0; i--) {
for (let j = arr[i].length - 1; j >= 0; j--) {
console.log(arr[i][j]);
}
}
Upvotes: 2