let arr1 =[1,2,3,4,5]
console.log(arr1.pop()) //5
console.log(arr1) //[ 1, 2, 3, 4 ]
let arr =[1,2,3,4,5]
console.log(arr.shift()) //1
console.log(arr) //[ 2, 3, 4, 5 ]
let arr =[1,2,3,4,5]
console.log(arr.splice(0,2)) //[ 1, 2 ]
console.log(arr) //[ 3, 4, 5 ]
let arr =[1,2,3,4,5]
console.log(arr.unshift(9)) //6 返回长度
console.log(arr) //[ 9, 1, 2, 3, 4, 5 ]
let arr =[1,2,3,4,5]
console.log(arr.push('name')) //6
console.log(arr) //[ 1, 2, 3, 4, 5, 'name' ]
let arr = [1,3,4,2,3,123,35,65,44,333,33]
//正序输出
let resArr = arr.sort((a,b)=>{
return a - b;
})
console.log(resArr);//[1,2,3,3,4,33, 35, 44, 65, 123, 333]
console.log(arr) //[1,2,3,3,4, 33,35, 44, 65, 123, 333]
let arr1 = [{id:2},{id:13},{id:6},{id:21},{id:5},{id:3}];
//正序
let resArr1 = arr1.sort((a,b)=>{
return a.id - b.id;//这里可以直接写进参数里
});
console.log(resArr1 ) //[ { id: 2 }, { id: 3 }, { id: 5 }, { id: 6 }, { id: 13 }, { id: 21 } ]
console.log(arr1) // [ { id: 2 }, { id: 3 }, { id: 5 }, { id: 6 }, { id: 13 }, { id: 21 } ]
let arr =[1,2,3,4,5]
console.log(arr.reverse()) //[ 5, 4, 3, 2, 1 ]
console.log(arr) //[ 5, 4, 3, 2, 1 ]
let arr =['a','b','c']
arr.forEach((item,index,obj) =>{
console.log(item,index,obj)}
)
let arr =['a','b','c']
arr.forEach((item,index,obj) =>{
// console.log(item,index,obj)
console.log(this)
})
let arr =['a','b','c']
let ren = arr.forEach((item,index,obj) =>{
// console.log(item,index,obj)
// console.log(this)
return item
})
console.log(ren)
let arr =[1,2,3]
let ren = arr.forEach((item,index,obj) =>{
console.log(item,index,obj)
obj[index] = item + 1
})
console.log(arr)
let arr =[1,2,3]
let ren = arr.map((item,index,obj) =>{
// console.log(item,index,obj)
console.log(this)
})
let arr =[1,2,3]
let ren = arr.map((item,index,obj) =>{
return item +1
})
console.log(arr)
console.log(ren)
let arr =[1,2,3]
let ren = arr.map((item,index,obj) =>{
console.log(item,index,obj)
})
let arr =[1,2,3,4,5]
console.log(arr.slice(1,3)) //[ 2, 3 ]
console.log(arr) //[ 1, 2, 3, 4, 5 ]
let arr =[8,9,3]
let res = arr.some((item,index,obj) =>{
return item >5
})
console.log(res) //true
let res1 = arr.some((item,index,obj) =>{
return item >10
})
console.log(res1) //false
console.log(arr) //[ 8, 9, 3 ] 不会改变原数组
let arr =[8,9,3]
let res = arr.every((item,index,obj) =>{
return item >5
})
console.log(res) //false ,如果数组中检测到有一个元素不满足,则整个表达式返回 false
let res1 = arr.every((item,index,obj) =>{
return item >2
})
console.log(res1) //true 如果所有元素都满足条件,则返回 true
console.log(arr) //[ 8, 9, 3 ] 不会改变原数组
let arr1 = [1, 2, 3, 4, 5];
let num = arr1.find(item => item > 1);
console.log(num) //輸出的結果是2
console.log(arr1) //[ 1, 2, 3, 4, 5 ]
var arr = [{
id: 1,
name: '一',
}, {
id: 1,
name: '二',
}, {
id: 2,
name: '三',
}]
let obj = arr.find(item => item.id == 1)
let obj1 = arr.find(item => item.id == 6)
console.log(obj); //{ id: 1, name: '一' }
console.log(obj1) //undefined //没有符合条件的元素,则返回undefined
console.log(arr) //[ { id: 1, name: '一' }, { id: 1, name: '二' }, { id: 2, name: '三' } ]
let arr1 = [1, 2, 3, 4, 5];
let num = arr1.filter(item => item > 1);
console.log(num) //[ 2, 3, 4, 5 ]
console.log(arr1) //[ 1, 2, 3, 4, 5 ]
let num1 = arr1.filter(item => item > 6);
console.log(num1) //[]
var arr = [{
id: 1,
name: '一',
}, {
id: 1,
name: '二',
}, {
id: 2,
name: '三',
}]
let obj1 = arr.filter(item => item.id >1)
let obj2 = arr.filter(item => item.id >3)
console.log(obj1) //[ { id: 2, name: '三' } ]
console.log(obj2)//[]
console.log(arr)//[ { id: 1, name: '一' }, { id: 1, name: '二' }, { id: 2, name: '三' } ]
let arr= ['1','2','3'];
console.log(arr.includes('1')); //true
console.log(arr.includes('7')); //false
let c =null
let u = undefined
console.log(c.reverse()) //报错TypeError
console.log(u.reverse()) //报错TypeError
let a =[null]
let b =[ undefined]
console.log(a.reverse()) // 输出结果:[ null ]
console.log(b.reverse()) // 输出结果:[ undefined ]
let e =[]
console.log(e.reverse()) // 输出结果:[]
let arr =[false,undefined,null]
console.log(arr.reverse()) //输出结果 [ null, undefined, false ]
console.log(arr) //输出结果 [ null, undefined, false ]
let arr1 =['a','b','c',true]
let arr2 =['a','b','',true]
console.log(arr1.reverse()) //输出结果[ true, 'c', 'b', 'a' ]
console.log(arr1)// 输出结果 [ true, 'c', 'b', 'a' ]
console.log(arr2) //输出结果 [ 'a', 'b', '', true ]
let arr1 =[1,2,3]
let arr2=['a','b','c',null]
let arr3 =['name','','age']
console.log(arr1.concat(arr2,arr3)) //[1,2,3,'a','b', 'c',null,'name', '', 'age']
let s = ['a','p','p','l','e']
console.log(s.join('')) // apple
let s = ['Apple','is','on','my','like']
console.log(s.join(' ')) // 输出结果: Apple is on my like
let s = ['a','b','c']
console.log(s) // 输出结果为:[ 'a', 'b', 'c' ]
console.log(s.join()) //输出结果为:a,b,c
持续更新中。。。