用嵌套循环JavaScript逆转子阵列

发布于 2025-02-09 11:12:22 字数 326 浏览 2 评论 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].lenght - 1; j > 0; j--)
      {
        console.log(arr[i][j]);
      }
  }

结果应该是9,8,7,6,5,4,3,2,1,但是我什么也没空。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘东忘西忘不掉你 2025-02-16 11:12:22
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);

您也可以使用降低
让我知道它是否适合您

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

够运 2025-02-16 11:12:22

您在单词length中都有错别字,并且应该是> = 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]);
    }
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文