陣列方法(every some filter includes find findIndex)
阿新 • • 發佈:2021-01-06
技術標籤:陣列javascript
every 遍歷陣列每一項,每一項返回true,最終結果為true.有一項返回false,停止遍歷,結果返回為false。不改變原陣列。
some 判斷陣列中是否存在滿足的條件,若存在返回true,不存在返回false
filter 過濾掉陣列中不滿足條件的值,返回一個新陣列,不改變原陣列
includes 判斷陣列中是否包含某一個元素,若包含返回true,否則返回true
find 查詢陣列中符合條件的第一個元素並返回該元素
findIndex 查詢陣列中符合條件的第一個元素的索引
{
let arr = [1,2, 3,4];
let arr1 = arr.every((item,index,arr)=>{
return item > 0;
});
console.log(arr1); //true
}
{
let arr = ["z","g","j"];
let arr1 = arr.some(function(m,index,arr){
return m == "z"
})
console.log(arr1) //true
}
{
let arr = ["z","g","j"]
let arr1 = arr.filter(function(m,index,arr){
return m != "z"
})
console.log(arr1) //["g", "j"]
}
{
let arr = ["z","g","j"]
let arr1 = arr.includes("z")
console.log(arr1) ; //true
}
{
let arr = [1,33,44,22,6,9]
let arr1 = arr.find(v => v > 10)
console.log(arr1) //33
}
{
let arr = [1,33,44,22,6,9]
let arr1 = arr.findIndex(v => v > 10)
console.log(arr1) //1
}