push/unshift/splice(这三个会改变原来的数组),concat不会改变原来的数组。
let a = [15,20,25,8,9] let a1 = a.push(66) console.log(a); //[ 15, 20, 25, 8, 9, 66 ] console.log(a1); //6
let b = ["小红","小李","小明"] let b1 = b.unshift("小王") console.log(b); //[ 小王, 小红, 小李, 小明 ] console.log(b1); //4
let c = ["m","n","l","b","t"] let c1 = c.splice(1,0,"q","r") console.log(c); //[m, q, r,n, l, b,t] console.log(c1); //[]
let d = [10,20,20,30,70] let d1 = d.concat([5,15]) console.log(d); //[ 10, 20, 20, 30, 70 ] console.log(d1); //[10, 20, 20, 30,70, 5, 15]
pop shift splice(前三种会改变原来的数组), slice不会改变原来的数组。
let a = [喂,你,好,啊] let a1= a.pop() console.log(a); //[ 喂, 你, 好 ] console.log(a1); //啊
let b = [10,20,50,80,30] let b1 = b.shift() console.log(b); //[ 20, 50, 80, 30 ] console.log(b1); //10
let c = [a,b,c,d,e] let c1 = c.splice(0,2,f) console.log(c); //[ f, c, d, e ] console.log(c1); //[ a, b ]
let d = [H,e,e,l,o] let d1 = d.slice(0,2) console.log(d) //[ H, e, e, l, o ] console.log(d1); //[ H, e ]
indexOf、find、findIndex、includes
let a = [10,20,50,70,60] let a1 = a.indexOf(20) console.log(a); //[ 10, 20, 50, 70, 60 ] console.log(a1); //1
indexOf方法第一个参数为要查找元素的值,也可以接收另外一个参数,该参数为元素的索引,也就是要从第几个索引开始查找,如果第二个参数不写,就默认从索引0开始。
let b = [10,20,50,70,60] let b1 = a.indexOf(20,2) console.log(b); //[ 10, 20, 50, 70, 60 ] console.log(b1); //-1
let c = [10,20,30,40] let c1 = c.find((item,index,arr)=>item>=20) console.log(c) //[ 10, 20, 30, 40 ] console.log(c1) //20
let d = [10,50,80,70,60] let d1 = d.findIndex((item,index,arr)=>item>=60) console.log(d) //[ 10, 50, 80, 70, 60 ] console.log(d1) //2
let e = [5,10,8,9,20] let e1 = e.includes(10,2) let e2 = e.includes(10) console.log(e1) //false console.log(e2); //true
let a = [10,20,50,70,20,60] let a1 = a.indexOf(20) console.log(a); //[ 10, 20, 50, 70, 20, 60 ] console.log(a1); //4
splice方法前面有讲
var arr = [1,2,3,4,5]; arr.splice(2, 1, 10); console.log(arr); //[1, 2, 10, 4, 5]
let a = [h,e,l,l,o] let a1 = a.join(,) console.log(a) //[ h, e, l, l, o ] console.log(a1) //h,e,l,l,o
let b = [{ age:20},{ age:50},{ age:30}] let b1 = b.filter(item=>item.age>30) console.log(b) //[ { age: 20 }, { age: 50 }, { age: 30 } ] console.log(b1) //[ { age: 50 } ]
let c = [10,15,20,25,30] let d = [a,b,c,d] let c1 = c.reverse() let d1 = d.reverse() console.log(c) //[ 30, 25, 20, 15, 10 ] console.log(c1) //[ 30, 25, 20, 15, 10 ] console.log(d) //[ d, c, b, a ] console.log(d1) //[ d, c, b, a ]
let e = [10,15,20,30,50] let e1 = e.some(item=>item>20) let e2 = e.some(item=>item>50) console.log(e) //[ 10, 15, 20, 30, 50 ] console.log(e1) //true console.log(e2) //false
let f = [10,20,50,80,56] let f1 = [] f.forEach(function(item){ f1.push(item) }) console.log(f); //[ 10, 20, 50, 80, 56 ] console.log(f1) //[ 10, 20, 50, 80, 56 ]
let g = [10,15,20,25,35] let g1 = g.map(item=>item*2) console.log(g) //[ 10, 15, 20, 25, 35 ] console.log(g1) //[ 20, 30, 40, 50, 70 ]
let h = [10,15,30,50,80] let h1 = h.every(item =>item>30) let h2 = h.every(item =>item>5) console.log(h) //[ 10, 15, 30, 50, 80 ] console.log(h1) //false console.log(h2) //true
let i = [10,15,20,30,50] let i1 = i.reduce(function(accumulator,item,currentIndex,arr){ return accumulator+= item },0) console.log(i) //[ 10, 15, 20, 30, 50 ] console.log(i1) //125