const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
console.log(list[2]) //就是拿到了33
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
list.push(100)
console.log(list)
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
list.unshift(0)
console.log(list)
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.pop()
console.log(result)
console.log(list)
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.shift()
console.log(result)
console.log(list)
前两个参数为被切数的索引,这个取头并取尾,接下来的参数就是替换别切的数
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.splice(1, 4, 666, 777, 888)
console.log(result)
console.log(list)
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.slice(1, 4)
console.log(result)
console.log(list)
拼接的可以是数组或者是其他数据类型(不包括对象)
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
const list2 = [100, 200]
let result = list.concat(300, list2)
console.log(result)
console.log(list)
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.reverse()
console.log(result)
console.log(list)
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.join("-")
console.log(result)
console.log(list)
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.toString()
console.log(result)
console.log(list)
找到第一个元素循环直接结束,没找到返回-1
const list = [11, 22, 33, 44, 55, 66, 66, 66, 77, 88, 99]
let result = list.indexOf(66)
console.log(result)
console.log(list)
手动实现
const list = [11, 22, 33, 44, 55, 66, 66, 66, 77, 88, 99]
Array.prototype.myIndexOf = function (res) {
for (let i = 0; i < this.length; i++) {
if (list[i] === res) {
return i
}
}
return -1
}
const result = list.myIndexOf(66)
console.log(result)
const list = [11, 22, 33, 44, 55, 66, 66, 66, 77, 88, 99]
let result = list.lastIndexOf(66)
console.log(result)
console.log(list)
const list = [11, 22, 33, 44, 55, 66, 66, 66, 77, 88, 99]
let result = list.sort((a, b) => a - b) //小到大
let result2 = list.sort((a, b) => b - a) //大到小
console.log(result)
console.log(result2)
console.log(list)
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let count = 0
list.forEach((value, index, array) => {
count += value //每一个元素
console.log(index) //索引
console.log(array) //数组
})
console.log(count)
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.filter(value => value > 60)
console.log(result)
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.reduce((pre, value) => pre + (value > 60 ? 1 : 0), 0)
console.log(result)
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.some(value => value > 60)
console.log(result)
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.every(value => value > 60)
console.log(result)
const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.map(value => value + 1)
console.log(result)