如何检查嵌套数组是否包含值? [英] How can I check If a nested array contains a value?
本文介绍了如何检查嵌套数组是否包含值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有这个数组:
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
}
我要做的就是检查这款手表是否具有特定的运动,如果确实有,则将其放在if语句中,以便我可以对其进行处理.
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.
我该怎么做?谢谢
推荐答案
使用 Array.indexOf 像下面这样
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屋!
查看全文