与 isEqual 有问题

问题描述 投票:0回答:1

我试图让它检测二维数组中的元素是否都相等,但无论它们是否相等,我都对它们保持“真”。

const isEqual = (currentValue) => currentValue = 5
const r = 0

array = [
  [1,2,3,4],
  [5,5,5,5],
  [6,7,8,9],
  [10,11,12,13]
];

for (i = 0; i < array.length; i++) {
      result = (array[i].every(isEqual));
      if(result == true) {
          r == i + 1
      }
   console.info("Row " + [i] + " has all equal values.")
}

并期待它说第 2 行具有所有相等的值。

但是我得到了这个

Row 0 has all equal values.
Row 1 has all equal values.
Row 2 has all equal values.
Row 3 has all equal values.
javascript multidimensional-array
1个回答
0
投票
  • 你的 isEqual 函数应该使用比较运算符 (===) 而不是赋值运算符 (=)
  • 您需要更新 console.info() 调用以仅在结果为真时执行

const isEqual = (currentValue) => currentValue === 5;
let r = 0;

array = [
  [1, 2, 3, 4],
  [5, 5, 5, 5],
  [6, 7, 8, 9],
  [10, 11, 12, 13],
];

for (let i = 0; i < array.length; i++) {
  result = array[i].every(isEqual);
  if (result === true) {
    r = i + 1;
    console.info("Row " + [i] + " has all equal values.");
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.