Array traversal methods commonly used in JS

1. Connect the array: arr.concat(), the return value is the connected array

        //concat()  连接数组
        let arr1 = [12, 32, 54]
        arr1 = arr1.concat(32, 43, 65)
        console.log(arr1); // [12, 32, 54, 32, 43, 65]

2. arr.join('separator'): connect each element in the array into a string

        //arr.join()  把数组中的每一个元素连接成字符串
        let arr1 = [12, 32, 54]
        arr1 = arr1.join()
        console.log(arr1); // 12,32,54  =>字符串,不是数字类型了

3. arr.reverse() : flip the array

        //arr.reverse()  翻转数组
        let arr1 = [12, 32, 54]
        arr1 = arr1.reverse()
        console.log(arr1); // [54, 32, 12]

4. arr.sort() : array sorting

        //arr.sort()  数组排序
        let arr1 = [12, 32, 54, 54, 32, 11]
        arr1 = arr1.sort(function (a, b) {
            // return a - b  //从小到大
            return b - a  //从大到小
        })
        console.log(arr1);

5. arr.reduce() => can be used to find the cumulative sum of arrays

        let arr = [21, 43, 21, 43];
        //第一个值为累加和变量
        //第二个值为当前元素
        //第三个当前下标
        let sum = arr.reduce((sum, value, index) => {
            return sum + value;
        }, 0);
        console.log(sum);  //128

6. Array deduplication arr.set()

        let arr = [20, 12, 30, 12, 56, 16, 16]
        let set = new Set(arr) //20, 12, 30, 56, 16  此时set不是一个真数组
        //将set转回数组
        let newArr = [...set] //这样就得到了一个经过去重后的新数组了
        console.log(newArr);

7. arr.map() => Get a new array after mapping the array

        let arr = [20, 12, 30, 12, 56, 16, 16]
        let newArr = arr.map((item, index) => {
            return item * 0.8  //需要return 返回值
        })
        console.log(newArr);
        
                //简写
        let newArr = arr.map(item => item * 0.8)
        console.log(newArr);

8. arr.filter() => filter array

        let arr = [12, 56, 16, 16, 34, 11, 17]
        let newArr = arr.filter((item, index) => {
            if (item % 2 == 0) {
                return true //return true 满足筛选条件,当前元素放入新数组中
            }
        })
        console.log(newArr);

        //简写
        let arr = [12, 56, 16, 16, 34, 11, 17]
        let newArr = arr.filter(item => item % 2 == 0)
        //return true 满足筛选条件,当前元素放入新数组中
        //return false 不满足筛选条件,当前元素不放入新数组中
        console.log(newArr);

9 arr.forEach() => for array loop

        //用于数组循环,可以替代for循环
        let arr = [20, 61, 80, 95, 100]

        arr.forEach((item, index) => {
            console.log(item, index);//没有return 
        })

10. arr.some() => determine whether there are elements in the array that meet the conditions

        let arr = [20, 61, 80, -95, 100]
        // 判断数组中有没有负数
        let res = arr.some((item, index) => {
            if (item < 0) {
                return true
            }
        })
        console.log(res);//true代表有负数    false代表没有负数

11. arr.every() => judge whether all elements in the array meet the conditions

        let arr = [20, 61, 80, -95, 100]
        // 判断数组中是否所有元素都是正数
        let res = arr.every((item, index) => {
            if (item > 0) {
                return true
            }
        })
        console.log(res);//true代表都是正数    false代表不都是正数,有负数

12. arr.findIndex() => Find the subscript of the reference type element

        //arr.findIndex用于找数组引用类型的下标
        let arr = [
            { name: '张三', age: 20, sex: '男' },
            { name: '李四', age: 21, sex: '男' },
            { name: '王五', age: 22, sex: '女' },
        ]
        let res = arr.findIndex((item, index) => {
            return item.name == "王五"
        })
        console.log(res);

Guess you like

Origin blog.csdn.net/m0_67296095/article/details/124679382