dimanche 1 février 2015

Better way to do nested for clauses


I have an array of data that I am going through, as well as an array of integers that represent individual items of the data array. My goal is to go through the data array one by one, and if the count of the item is in the integer array one thing will happen, and if it's not, another thing will happen.


To do this, I created two nested for clauses with an if statement and a Boolean toggle (representation of the code below). My question is whether there are other ways to do this, and what might be the advantage of doing it a different way (for example, if the data array gets big)?



var fp=false; //toggle
var data=array; //array of data
var tog=array; //integer array for example, [7;22;53;2]

for (i=0; i<data.length; i++){
for(j=0; j<tog.length; j++){
if(i==tog[j]){
fp=true;
doSomething(i);
};
};
if(fp==false){
doSomething2(i);
}
else{
fp=false;
};
};




Aucun commentaire:

Enregistrer un commentaire