在前端獲取數據以後,咱們常常須要對數據進行一些判斷,再作邏輯處理,本質其實就是須要一些方法返回布爾值,這篇文章咱們主要總結數組的一些方法巧用。前端
爲false的狀況:0 , ‘ ’, null, undefined, false數組
# Array.includes()spa
includes()
方法用來判斷一個數組是否包含一個指定的值,根據狀況,若是包含則返回 true,不然返回false。code
var array1 = [1, 2, 3]; console.log(array1.includes(2)); // expected output: true var pets = ['cat', 'dog', 'bat']; console.log(pets.includes('cat')); // expected output: true console.log(pets.includes('at')); // expected output: false
# indexOf()
方法返回在數組中能夠找到一個給定元素的第一個索引,若是不存在,則返回-1。對象
var beasts = ['ant', 'bison', 'camel', 'duck', 'bison']; console.log(beasts.indexOf('bison')); // expected output: 1 // start from index 2 console.log(beasts.indexOf('bison', 2)); // expected output: 4 console.log(beasts.indexOf('giraffe')); // expected output: -1
能夠用來這樣判斷blog
const articleType = (typeKey === null || typeKey.indexOf('status') > -1) ? '1' : 0;
# Array.pop()索引
pop()
方法從數組中刪除最後一個元素,並返回該元素的值。ip
從數組中刪除的元素(當數組爲空時返回undefined
)。作用域