简体   繁体   English

比较 arrays 项目时遇到问题

[英]Trouble on comparing arrays items

I cannot understand why my comparison between arrays items always return true.我不明白为什么我在 arrays 项目之间的比较总是返回 true。 How can I check if different items in the same array are not equal?如何检查同一数组中的不同项目是否不相等?

I tried != , !== , and .colCheck[i].equals(colCheck[x]) but it did not work.我试过!=!==.colCheck[i].equals(colCheck[x])但它没有用。

function mergeCells() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getActiveSheet();
  var rowsToMerge = [];
  var colCheck = sheet.getRange(8,10,79,1).getValues();

  for (var i = 0; i < 79; i++) {
    var x = i + 1;
    if (colCheck[i] != colCheck[x]) rowsToMerge.push(i);// Always returns true
    if (colCheck[i] == '') {
      break;
    }
  }
}

I checked the arrays items and even when they are exactly the same, the comparison returns true.我检查了 arrays 项目,即使它们完全相同,比较也会返回 true。

Elements of colCheck are objects, so comparing with == , != , === , or !=== just compares references which is probably not what you intend, try comparing object properties instead. colCheck 的元素是对象,因此与==!====!===比较只是比较可能不是您想要的引用,请尝试比较 object 属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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