4

I have a javascript variable that is an array of arrays. Then I have a variable below it. Like this:

var cars = [
    ["ford mustang",1955,"red"],
    ["dodge dart",1963,"green"],
    ["pontiac",2002,"green"],
]

var colour = "blue";

Now I need to check for if either the third values of each array are all the same as the variable, colour, or they are all different. If one of those conditions are true, I want to execute some other code. So in the above example, the condition would be true because none of the values are "blue". It would also be true if they were all "blue".

Hopefully I've made myself clear.

2
  • I can think of a number of ways to do this. You can count the number of matches, and see if it's 0 or cars.length. You can loop over the elements. After you find a match, then stop if you find a mismatch and report failure; after you find a mismatch, stop if you find a match. Commented Nov 12, 2013 at 1:21
  • 3
    You've already described the algorithm you need; take the next step and try to code it yourself. Commented Nov 12, 2013 at 1:22

4 Answers 4

9

There are two functions in JavaScript just for that:

allCarsAreRed = cars.every(function(car) { return car[2] == 'red' })
atLeastOneCarIsRed = cars.some(function(car) { return car[2] == 'red' })
noRedCars = cars.every(function(car) { return car[2] != 'red' })
Sign up to request clarification or add additional context in comments.

3 Comments

These do work nicely, but they don't work in IE8 or earlier so one would need a shim for them if you need to support IE8.
@jfriend00: yes, every time you mention a ES5 feature in a post, someone comes up with this IE8 comment. To be honest, I find these comments rather misleading. First, no one "needs" to support IE8, quite the contrary - the less we support it, the sooner it dies - to everyone's benefit. Second, and worse, these comments scare beginners away from modern javascript and promote outdated programming practices.
@thg435 - You don't get to decide whether someone needs to support IE8 or not. That is their decision. I just added some more info to your answer that might be useful if someone had a requirement to support IE8. Why does that additional info both you. I didn't downvote you - just added more info.
5
var colour = 'blue'
var all = true;
var none = true;
for (var i = 0; i < cars.length; i++) {
   if (cars[i][2] !== colour) {
      all = false;
   } else  {
      none = false;
   }

}

4 Comments

Use else instead of comparing twice.
You're still comparing twice. Just use else, not else if.
Thank you! I thought about setting the variables to false and changing them to true, but I couldn't figure out how that would work. This works awesome.
To @eshellborn, before using this answer just make sure that you obtain length of your array variable just once. Not every time when you iterate.
3

Do you need something like this?

var cars_length = cars.length;
var all_same = true;
var all_different = true;
for(var i=0; i<cars_length; i++)
{
    if(cars[i].[2] == colour)
    {
       all_same = false;
    }
    else
    {
       all_different = false;
    }
}

if(all_same)
{
   console.log('all same');
}
else
{
   if(all_different)
   {
       console.log('all different');
   }
   else
   {
       console.log('nor all same, nor all different');
   }
}

Comments

0

Look at bellow Solution

var arr = ["a", "b", "c", "d"]
var allZero = true;
for (var i = 0; i < arr.length; i++) 
{
    if (arr[i][2] != 0) 
    {
        allZero = false;
        break;
    }
}
if (allZero) 
{
    console.log("ALL are Zero");
}

1 Comment

this doesn't address the multidimensional aspect of the question

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.