js数组的所有方法

js数组方法


1.按索引取值(原数组不变化)

const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
console.log(list[2])    //就是拿到了33

2.后面添加值(原数组变化)

const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
list.push(100)
console.log(list)

3.前面添加值(原数组变化)

const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
list.unshift(0)
console.log(list)

4.取出后面的值(原数组变化)

const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.pop()
console.log(result)
console.log(list)

4.取出前面的值(原数组变化)

const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.shift()
console.log(result)
console.log(list)

5.数组的切片(原数组变化)

前两个参数为被切数的索引,这个取头并取尾,接下来的参数就是替换别切的数

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)

6.数组的切片(原数组不变化)

const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.slice(1, 4)
console.log(result)
console.log(list)

7.数组的拼接(原数组不变化)

拼接的可以是数组或者是其他数据类型(不包括对象)

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)

8.数组的翻转(原数组变化)

const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.reverse()
console.log(result)
console.log(list)

9.数组转化字符串(原数组不变化)

const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.join("-")
console.log(result)
console.log(list)

10.数组转化字符串(原数组不变化)

const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.toString()
console.log(result)
console.log(list)

11.查找索引(原数组不变化)

找到第一个元素循环直接结束,没找到返回-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)

12.查找最后索引(原数组不变化)

const list = [11, 22, 33, 44, 55, 66, 66, 66, 77, 88, 99]
let result = list.lastIndexOf(66)
console.log(result)
console.log(list)

13.数组的排序(原数组变化)

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)

14.数组的循环(原数组不变化)

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)

15.数组的过滤(原数组不变化)

const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.filter(value => value > 60)
console.log(result)

16.数组的计数(原数组不变化)

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)

17.判断是否存在满足条件的元素(原数组不变化)

const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.some(value => value > 60)
console.log(result)

18.判断每一个是否满足条件的元素(原数组不变化)

const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.every(value => value > 60)
console.log(result)

19.映射(原数组不变化)

const list = [11, 22, 33, 44, 55, 66, 77, 88, 99]
let result = list.map(value => value + 1)
console.log(result)

你可能感兴趣的:(JavaScript,前端,javascript,前端)