1中是否存在某个值。array.indexOf判断数组中是否存在某个值,如果存在数组元素下标,否则返回-1letarr=['something','anything','nothing','anything'];letindex=arr.indexOf('nothing');#Result:22.array.includes(searchElement[,fromIndex])判断一个数组是否包含指定值,存在则返回true,否则返回false。参数:searchElement需要查找的元素值。参数:thisArg(可选)开始搜索searchElement的索引。如果为负,则搜索从array.length+fromIndex的索引开始,按升序排列。默认为0。letnumbers=[12,5,8,130,44];letresult=numbers.includes(8);#result:trueresult=numbers.includes(118);#result:false3.array.find(callback[,thisArg])返回数组中第一个满足条件的元素的值,不满足则返回未定义参数:callbackelement当前遍历的元素。index当前遍历的索引。array数组本身。参数:thisArg(可选)指定回调的this参数。//----------元素是纯文字----------设数字=[12,5,8,130,44];letresult=numbers.find(item=>{returnitem>8;});#Result:12//------------元素是对象------------letitems=[{id:1,name:'something'},{id:2,name:'anything'},{id:3,name:'nothing'},{id:4,name:'anything'}];letitem=items.find(item=>{returnitem.id==3;});#Result:Object{id:3,name:"nothing"}4.array.findIndex(callback[,thisArg])返回数组中第一个满足条件的元素的索引(下标),如果没有找到则返回-1参数:callbackelement当前遍历的元素。index当前遍历的索引。array数组本身。参数:thisArg(可选)指定回调的this参数。//----------元素是纯文字----------设数字=[12,5,8,130,44];letresult=numbers.findIndex(item=>{returnitem>8;});#Result:0//------------元素是一个对象------------letitems=[{id:1,name:'something'},{id:2,name:'anything'},{id:3,name:'nothing'},{id:4,name:'anything'}];让index=items.findIndex(item=>{returnitem.id==3;});#结果:2
