2

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

Prabhu Murthy
9,2865 gold badges32 silver badges37 bronze badges
asked Aug 17, 2014 at 12:06
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"). Commented Aug 17, 2014 at 12:08
  • Have you tried Underscore? Commented Aug 17, 2014 at 12:08

6 Answers 6

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.

answered Aug 17, 2014 at 12:13
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Aug 17, 2014 at 12:13

Comments

0

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?

answered Aug 17, 2014 at 12:13

Comments

0

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.

answered Aug 17, 2014 at 12:24

Comments

0

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

answered Aug 17, 2014 at 12:41

Comments

0

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.

answered Oct 27, 2023 at 10:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.