简体   繁体   中英

Trouble on comparing arrays items

I cannot understand why my comparison between arrays items always return 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.

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.

Elements of colCheck are objects, so comparing with == , != , === , or !=== just compares references which is probably not what you intend, try comparing object properties instead.

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