if I have an array like this:
thisarray = new Array("this", "that", "theotherthing");
how could I go about building a conditional like so:
if(thisarray[0] == thisvar && thisarray[1] == thisvar && thisarray[2] == thisvar) {
//do something
}
the caveat is that I do not know how many items could be in thisarray. I'm a bit stumped as to how to acplish this. Any help would be appreciated.
if I have an array like this:
thisarray = new Array("this", "that", "theotherthing");
how could I go about building a conditional like so:
if(thisarray[0] == thisvar && thisarray[1] == thisvar && thisarray[2] == thisvar) {
//do something
}
the caveat is that I do not know how many items could be in thisarray. I'm a bit stumped as to how to acplish this. Any help would be appreciated.
Share Improve this question edited Jan 20, 2011 at 15:35 user1228 asked Jan 17, 2011 at 18:12 PruitIgoePruitIgoe 6,39417 gold badges79 silver badges145 bronze badges 9- When you say <code>thisvar</code> 3 times, do you really mean the same one? – crimson_penguin Commented Jan 17, 2011 at 18:15
- 1 If your array consist of different values, the statement will never be true anyway... – Felix Kling Commented Jan 17, 2011 at 18:27
- The way your question is asked, it sounds like the following: How can I check if all values in an array are a specific value? Is that what you're trying to do? – Ruan Mendes Commented Jan 17, 2011 at 18:31
- @c_p - no, sorry, no that's a mistake, it should be thisarray[0] == thisvar1 && thisarray[1] == thisvar2, etc. – PruitIgoe Commented Jan 17, 2011 at 18:42
-
@Pruitlgoe -- if you don't know how many items there are ahead of time, then where are your
thisvar1
,thisvar2
, etc... ing from? Do you just want to check if two arrays are equal? – Ben Lee Commented Jan 17, 2011 at 18:51
6 Answers
Reset to default 2If you have Javascript 1.6 support, you can do it in one line:
if (thisarray.every(function(e) { return (e == thisvar); })) {
// Do stuff
}
MDN reference
var valid = true;
for(var i=0; i<thisarray.length && valid; ++i){
if (thisarray[i] != thisvar)
valid = false;
}
//use valid
if(valid)
{
//Do your stuff
}
You can use a for loop to "iterate" through the items in the array, checking the value each time.
var result = true;
for(var x=0; x < thisarray.length; x+=1){
if(thisarray[x] != thisvar){
result = false;
}
}
result
will be true if every item of the array equals thisvar, false if there is a mismatch.
You could write a function:
function all(arr, f) {
for (var i = 0; i < arr.length; ++i)
if (!f(arr[i], i)) return false;
return true;
}
Then you can call:
if (all(thisArray, function(a) { return a === thisvar; })) {
// all equal
}
UPDATE: I mis-read your request to check if any of the elements match, when what you asked for is for all of them to match. I'm leaving my answer here though for reference if you want the condition to be true for any match.
You could just iterate through the elements with a for loop, like this:
for (var i = 0; i < thisarray.length; i++) {
if (thisarray[i] == thisvar) {
// do something
break; // so it doesn't repeat if there are multiple matches
}
}
Just have an array of actual values and an array of expected values and then pare elements. Something like this:
function arraysEqual(actual, expected) {
if (actual.length !== expected.length) { return false; }
var count = actual.length;
for (i = 0; i < count; i++) {
if (actual[i] !== expected[i]) { return false; }
}
return true;
}