最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: Check if all elements of an array are the same as a variable, or none of them are - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Nov 12, 2013 at 1:14 eshellborneshellborn 11.3k5 gold badges22 silver badges29 bronze badges 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. – Barmar 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. – musical_coder Commented Nov 12, 2013 at 1:22
Add a comment  | 

4 Answers 4

Reset to default 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' })
  • some

  • every

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

}

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');
   }
}

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");
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论