admin管理员组

文章数量:1296260

So I have this array:

   var watchesArray = [
        {
          model: "FR 10", 
          image:"",
          url: "",
          price: 129.99,
          sports: ["Running", "Fitness"],
          touchScreen:false,
          GPS:false,
          heartRateMonitor:false,
          hrZoneTraining:false,
        },
    ];

    if(watchesArray[0].sports(contains a specific value or values){
      do something
    } else {
       dont do anything
    }

And all I want to do is check whether this watch has a specific sport and if it does then put it in an if statement so that I can do something with it.

How can I do this? Thanks

So I have this array:

   var watchesArray = [
        {
          model: "FR 10", 
          image:"",
          url: "",
          price: 129.99,
          sports: ["Running", "Fitness"],
          touchScreen:false,
          GPS:false,
          heartRateMonitor:false,
          hrZoneTraining:false,
        },
    ];

    if(watchesArray[0].sports(contains a specific value or values){
      do something
    } else {
       dont do anything
    }

And all I want to do is check whether this watch has a specific sport and if it does then put it in an if statement so that I can do something with it.

How can I do this? Thanks

Share Improve this question edited Aug 17, 2014 at 12:49 Prabhu Murthy 9,2615 gold badges31 silver badges37 bronze badges asked Aug 17, 2014 at 12:06 Rafael ColonRafael Colon 271 gold badge1 silver badge7 bronze badges 2
  • 4 It's just an array, even if it's "nested": watchesArray[0].sports.indexOf('Running') !== -1 Note that this returns 0 if it's the first index, so you have to check for -1 (which is "not found"). – Jared Farrish Commented Aug 17, 2014 at 12:08
  • Have you tried Underscore? – An Phan Commented Aug 17, 2014 at 12:08
Add a ment  | 

6 Answers 6

Reset to default 3

Use Array.indexOf like bellow

if(watchesArray[0].sports.indexOf('Running') > -1){
    do somwthing
}

NOTE:- In case of if parents doesn't exist it'll throw error.

It's just an array, even if it's "nested":

if (watchesArray[0].sports.indexOf('Running') !== -1) ...

This returns 0 if it's the first index, so you have to check for -1 (which is "not found").

Note that IE7/8 did not support Array.prototype.indexOf(), but there are ways to polyfill that if needed.

There is no one simple function which supported in all browsers but you can write the following code:

function contains(a, obj) {
    var i = a.length;
    while (i--) {
       if (a[i] === obj) {
           return true;
       }
    }
    return false;
}

For more details you look in this answer How do I check if an array includes an object in JavaScript?

You can try indexof like this:

var watchesArray = [
                     {
                       model: "FR 10", 
                       image:"",
                       url: "",
                       price: 129.99,
                       sports: ["Running", "Fitness"],
                       touchScreen:false,
                       GPS:false,
                       heartRateMonitor:false,
                       hrZoneTraining:false,
                     },
                 ];

 if (watchesArray[0].sports.indexOf('hello') != -1){
                     alert("exists");
                 } else {
                     alert("this index does not exist");
                 }

for array in javascript you can always traverse using the key specified. if the key does not exists indexof will return 0 so it will go to the else part.

As Jared mentioned you can use Array.indexOf()

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

In your case it would mean

if(watchesArray[0].sports.indexOf(value) !== -1){
  do something
} else {
   dont do anything
}

I also provided you a little function to match multiple values: jsbin

Nowadays you can also use Includes.

if(watchesArray[0].sports.includes('Running'))
{
    // Exists
}
else{
    // Doesn't exist
}

This is not always supported though so check that you browser supports it.

本文标签: javascriptHow can I check If a nested array contains a valueStack Overflow