-2

I would like to ask a question regarding an IF statement.

Like what is stated from the title, how does the "for" and "If(x[i][0])" works? The code looks something like this.

var dataws1 = source.getRange(1,1,Alast,source.getLastColumn()).getValues(); 
var outData = [];
for (var i in dataws1) {
  if (dataws1[i][0] == criteria) { 
    outData.push(dataws1[i])
  }
}

From my understanding, the current IF checks the 1st column if it matches with the criteria, then places it inside the array of outData. I tried changing some of the values of the IF to check the other columns but to no avail. I'm still not familiar with Java scripting and I wish to learn more on how to read and on how it works. Thanks in advance!

  • 1
    Possible duplicate of [For-each over an array in JavaScript?](https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript) – Joseph Cho Jan 23 '19 at 14:47

2 Answers2

2

This loop is taking each item in dataws1 and then checking the first value in it against "criteria" and if it matches adding it to the outData array. So if dataws1 has 3 items it would be doing something like this:

if (dataws1[0][0] == criteria) { 
  outData.push(dataws1[i])
}
if (dataws1[1][0] == criteria) { 
  outData.push(dataws1[i])
}
if (dataws1[2][0] == criteria) { 
  outData.push(dataws1[i])
}

var criteria = "match";
var outData = [];
var dataws1 = [
  ['match', 'first'],
  ['nonMatch', 'second'],
  ['match', 'third']
];
for (var i in dataws1) {
  if (dataws1[i][0] == criteria) {
    outData.push(dataws1[i])
  }
}
console.log(outData)
Scath
  • 3,777
  • 10
  • 29
  • 40
2

Your code is copying/pushing every array from dataws1 which has "criteria" as its first element.

For example:

var dataws1 = [['criteria', 'hello'], ['what', 'is', 'criteria'], ['criteria', 'friend']];
var outData = [];
for (var i in dataws1) {
   if (dataws1[i][0] == 'criteria') { 
      outData.push(dataws1[i]);
   }
}
for (var j in outData) {
  document.write("outData["+j+"] = [" + outData[j] + "]<br>");
}

outData will contain outData = [[criteria, hello], [criteria, friend]]

Lev Buchel
  • 532
  • 5
  • 14
  • I see, That explains a lot. If the "criteria" happens to be on a different order on the array, say [hello, friend, criteria], does it still detect "criteria"? If not, what would be the possible workaround for it? – Engelbert Aaron Caro Jan 23 '19 at 14:58
  • I have updated the original answer and added "criteria" to the end of the second array in `dataws1`. As you can see it will not push that array to `outData` since "criteria" is not the first element there. You have many options you can do to include any array with "criteria" somewhere in there. I will mention 3 options: 1) change your `if` statement to `dataws1.includes(criteria)`. 2) change your `if` statement to `dataws1.indexOf(criteria)`. 3) You can also use a loop and go through every element in the array manually and search for "criteria". – Lev Buchel Jan 24 '19 at 06:35
  • 1
    Noted on that. Thank you so much for this Lev. – Engelbert Aaron Caro Jan 24 '19 at 13:44