- Home /
Check/compare array elements
Trying to check the elements of an array i have created with var newArr = new Array();
These array elements are pieces of text that i am using on buttons.
How can i check the contents of the array to see if one is still in it? The location of the text in the array doesnt stay constant so i cant check a specific spot in it. Here is what i tried but I dont think i have the right syntax.
var toolBeltList = new Array();
function ReplaceTool()//when called check the array elements for this text, if false do something { if(toolBeltList!=("Tool01")) { box01=true; }
}
Answer by Eric5h5 · Apr 05, 2010 at 11:20 PM
You have to check each element in the array (unless you find it, in which case you can stop checking):
box01 = true;
for (tool in toolBeltList) {
if (tool == "Tool01") {
box01 = false;
break;
}
}
Or you can use ArrayList instead of Array, which is basically the same sort of thing, but it does have a Contains method:
if (!toolBeltList.Contains("Tool01")) {
box01 = true;
}