日常生活的交流与学习

首页 新随笔 联系 管理

以行进行遍历

const arrList = [
  ["100", "200", "300"],
  ["400", "500", "600"],
  ["700", "800", "900"],
];
// for 循环的初始值,控制着起始位置,约束条件控制着结束位置
for (let i = 0; i < 3; i++) {
  for (let j = i; j < 3; j++) {
    console.log(arrList[i][j]);
  }
}
console.log("---------------------");
for (let i = 0; i < 3; i++) {
  for (let j = 0; j <= i; j++) {
    console.log(arrList[i][j]);
  }
}
console.log("-----------------------");
for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3 - i; j++) {
    console.log(arrList[i][j]);
  }
}
console.log("----------------------");
for (let i = 0; i < 3; i++) {
  for (let j = 2 - i; j < 3; j++) {
    console.log(arrList[i][j] );
  }
}


以列进行遍历


const arrList = [
  ["100", "200", "300"],
  ["400", "500", "600"],
  ["700", "800", "900"],
];

for (let j = 0; j < 3; j++) {
  for (let i = 0; i <= j; i++) {
    console.log(arrList[i][j]);
  }
}

console.log("------------------------");
for (let j = 0; j < 3; j++) {
  for (let i = j; i < 3; i++) {
    console.log(arrList[i][j]);
  }
}

console.log("-----------------------------");
for (let j = 0; j < 3; j++) {
  for (let i = 0; i < 3 - j; i++) {
    console.log(arrList[i][j]);
  }
}

console.log("----------------------");
for (let j = 0; j < 3; j++) {
  for (let i = 2 - j; i < 3; i++) {
    console.log(arrList[i][j]);
  }
}


posted on 2022-10-20 09:26  lazycookie  阅读(27)  评论(0编辑  收藏  举报