简体   繁体   中英

reversing sub arrays with nested loop Javascript

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.

 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

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]); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM