Mar*_*sen 5

在javascript中你有阵列(列表)和对象(地图).

它们的文字版本如下所示:

var mylist = [1,2,3]; // array
var mymap = { car: 'porche', hp: 300, seats: 2 }; // object
Run Code Online (Sandbox Code Playgroud)

如果你要弄清楚数组中是否存在一个值,只需循环遍历它:

for(var i=0,len=mylist.length;i<len;i++) {
  if(mylist[i] == 2) {
     //2 exists
     break;
   }
}
Run Code Online (Sandbox Code Playgroud)

如果你要弄清楚地图是否有某个键,或者它是否有一个具有特定值的键,你所要做的就是这样访问它:

if(mymap.seats !== undefined) {
  //the key 'seats' exists in the object
}

if(mymap.seats == 2) {
  //the key 'seats' exists in the object and has the value 2
}
Run Code Online (Sandbox Code Playgroud)


Clo*_*ble 5

Array.indexOf(element) 如果未找到element,则返回-1,否则返回其索引