0

This snippet of code is throwing me for a loop.

if(colList[i] != checkList[i]) {
  var colTest = colList[i];
  var checkTest = checkList[i];

As you can see from this screenshot from the debug the values are identical. ScreenShot

Any hints as to why the if statement thinks these values are different?

EDIT: Here is a screenshot showing the full arrays.

Again, I'm not sure why this matters. In fact for testing purposes I have both arrays pulling from the exact same source data.

2nd Edit:

Here is all the relevant code. Again, as you can see the arrays are identical.

var colList = sheet.getRange(startRow,watchCol,lastRow,1).getValues(); // Data set with all values to watch
var checkList = sheet.getRange(startRow,watchCol,lastRow,1).getValues(); // Data set with all the check values

function timeStamp() {
  for(var i = 0; i <= colList.length; i++)
    if(colList[i] != checkList[i]) {
      return colList
      return checkList

Here is the full code that is trying to treat it as a multidimensional array. This code does not work and returns "Cannot read property "0" from undefined. (line 13,"

var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
var startRow = 2; // First row with Data
var lastRow = sheet.getLastRow() - startRow;
var watchCol = 2; // Column to check for changes
var checkCol = 7; // Column to check against
var timeCol = 3; // Column to put the time stamp in
var colList = sheet.getRange(startRow,watchCol,lastRow,1).getValues(); // Data set with all values to watch
var checkList = sheet.getRange(startRow,watchCol,lastRow,1).getValues(); // Data set with all the check values

function timeStamp() {
  for(var i = 0; i <= colList.length; i++)
  for(var j = 0; j < checkList.length; j++){
    if(colList[i][j] != checkList[i][j]) {
      return colList
      return checkList
      sheet.getRange(i + startRow,checkCol).setValue(colList[i]);
      sheet.getRange(i + startRow,timeCol,1,1).setValue(new Date());
    }
    }
}
Thaaron
  • 21
  • 6
  • 5
    Where is the array..... Please include all **relevant** source code. Example: `colList` and `checkList` – NewToJS Jul 27 '17 at 20:00
  • We just see that the two arrays hold the same value. It doesn't mean it's the same. – Denys Séguret Jul 27 '17 at 20:00
  • 1
    Please post the relevant arrays into the question. – War10ck Jul 27 '17 at 20:01
  • I'm not sure what you mean. I'm comparing only 1 value from each array which the debug shows are equal. – Thaaron Jul 27 '17 at 20:02
  • 1
    I am guessing there is a hidden character in one of them: `console.log(escape(colList[i])); console.log(escape(checkList[i]))` – epascarello Jul 27 '17 at 20:03
  • @Thaaron read at the QA I linked to. Two arrays holding the same values aren't the same array. – Denys Séguret Jul 27 '17 at 20:03
  • Probably why you should use object, not array. – jdmdevdotnet Jul 27 '17 at 20:03
  • 2
    What does arrays have to do with it.... OP is saying string1 and string2 appear to be the same, but they are not. – epascarello Jul 27 '17 at 20:03
  • @epascarello how so? – jdmdevdotnet Jul 27 '17 at 20:04
  • 1
    the `colTest[i]` and `checkTest[i]` are not holding string values but I think they are arrays. – Muhammad Jul 27 '17 at 20:06
  • Correction: they **look** identical. Expand each string into an array of characters and inspect the int value of each character (using code @epascarello suggested). I suspect one of the strings contains a zero-width Unicode character that isn't visible through the debugger visualizer. –  Jul 27 '17 at 20:09
  • 3
    We should probably stop trying to answer this question without the data. Save yourself the trouble and encourage the asker to give actual code. – Domino Jul 27 '17 at 20:10
  • @JacqueGoupil I couldn't agree more. I did ask for all relevant source to be included but it appears the OP doesn't intend to include it. – NewToJS Jul 27 '17 at 20:13
  • you are using `return` in your if statement immediately and it makes the statements after that unreachable, so the lines after that will be meaning less. – Muhammad Jul 27 '17 at 21:05

3 Answers3

0

As I said in the comment, you have that one index arrays inside another array, so yours are multi-dimensional arrays and you have to use 2 indexes to access its values, i = row and j = column

var checkList = [["Beef"], ["Red"], ["Career"], ["Chicken"], ["Red"], ["Kids"], ["Beef"], ["Red"]];
var colList = [["Beef"], ["Red"], ["Career"], ["Chicken"], ["Red"], ["Kids"], ["Beef"], ["Red"]];
function timeStamp() {
  for(var i = 0; i < colList.length; i++){
  for(var j = 0; j < checkList[i].length; j++){
    if(colList[i][j] != checkList[i][j]) {
            console.log('not equal');
        } else{
        console.log('equal');
      }
    }
  }
}

timeStamp();
Muhammad
  • 6,725
  • 5
  • 47
  • 54
  • As you can see from the screenshot they are not multidimensional arrays. When I tried your code is returns "j" is not defined. – Thaaron Jul 27 '17 at 20:35
  • @Thaaron you need two loops, one to iterate over the rows and another for columns. let me give you an example. – Muhammad Jul 27 '17 at 20:37
  • I added the relevant parts but get " Cannot read property "0" from undefined." I really don't think there's a 2nd dimension for it to read... – Thaaron Jul 27 '17 at 20:58
  • @Thaaron you know `[['Red']]` is a multi dimensional array. when you enclose a string, number, boject or array in [ and ] it become an array. – Muhammad Jul 27 '17 at 21:00
0

According to your screenshots it's simple.

Your's arrays doesn't contain strings, they contain array that contain string, and thus to compare is true, because two arrays will always be different, that because arrays in js are objects and when you try to compare objects it compares that references of them , not the value.

So you should make array of strings, or just to add [0] to each side in the if

Nati V
  • 682
  • 4
  • 10
  • 17
0

As it turns out adding .String() at the end of the function creating the arrays fixed the issue and allowed them to compare correctly.

Thaaron
  • 21
  • 6