2

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 accomplish this. Any help would be appreciated.

9
  • When you say <code>thisvar</code> 3 times, do you really mean the same one? Commented Jan 17, 2011 at 18:15
  • 1
    If your array consist of different values, the statement will never be true anyway... 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? 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. 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... coming from? Do you just want to check if two arrays are equal? Commented Jan 17, 2011 at 18:51

6 Answers 6

2

If 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

Sign up to request clarification or add additional context in comments.

Comments

1
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
}

Comments

1

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.

1 Comment

See my edit to the original post, I made a mistake in typing it. Each array item will match a unique var.
1

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
}

1 Comment

Maybe it's a bit overkill, but it's pretty clean. :)
0

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
    }
}

2 Comments

I was going to up vote this, but then I realized that he wants to do something if ALL of them are true, not just one.
Sorry, I wasn't clear. I am trying to match different array items with different variables, in my OP i messed that up. I won't know how many items might be in the array which is where I am getting stumped...see my edit above...
0

Just have an array of actual values and an array of expected values and then compare 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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.