JavaScript Study Notes (9) Array Objects

Table of contents

1. Two ways to create an array

1. Using array literals

2. Use new Array()

2. Check if it is an array

1. The instanceof operator, which can be used to detect whether it is an array

2. Array.isArray(parameter)

3. Add and delete array elements

1. push() adds one or more array elements at the end of the array

 2. arr.unshift() adds one or more array elements at the beginning of the array, and returns the length of the new array

3. pop() deletes the last element of the array and returns the value of the deleted element

4. shift() deletes the first element of the array and returns the value of the first element

4. Array index

Five, the case - array deduplication

 6. Convert array to string

7. Others


1. Two ways to create an array

1. Using array literals

        //创建数组
        // 1.数组字面量
        var arr = [1,2,3]
        console.log(arr[0])

2. Use new Array()

        //2. 利用new Array()
        var arr1 = new Array(2);//这个2表示数组长度为2,里面是两个空数组元素
        var arr2 = new Array(1,2,3)
        console.log(arr1);
        console.log(arr2);

2. Check if it is an array

1. The instanceof operator, which can be used to detect whether it is an array

        var arr = [];
        var obj = {}
        console.log(arr instanceof Array)//true
        console.log(obj instanceof Array)//False

2. Array.isArray(parameter)

        //2.Array.isArray   H5新增的方法,ie9以上支持
        console.log(Array.isArray(arr))//true
        console.log(Array.isArray(obj))//False

3. Add and delete array elements

1. push() adds one or more array elements at the end of the array

After the push is completed, the length of the new array is returned, and the original array will also change

        //添加删除数组元素
        var arr3 = [1,2,3];
        // arr.push(4,'新增')
        console.log(arr3.push(4,'新增'))
        console.log(arr3)

 2. arr.unshift() adds one or more array elements at the beginning of the array, and returns the length of the new array

        //arr3.unshift('前面','新增')
        console.log(arr3)
        console.log(arr3.unshift('前面','新增'))
        console.log(arr3)

3. pop() deletes the last element of the array and returns the value of the deleted element

4. shift() deletes the first element of the array and returns the value of the first element

        //删除元素
        console.log(arr3.pop())
        console.log(arr3)
        console.log(arr3.shift())
        console.log(arr3)

4. Array index

indexOf(array element)

Returns the first index number that satisfies the condition, and searches from the front. If it cannot find an element in the array, it returns -1

lastIndexOf (array element), start searching from the back

Five, the case - array deduplication

        //数组去重,封装一个去重的函数unique
        function unique(arr){
            var newArr = [];
            for (var i= 0;i<arr.length;i++){
                if(newArr.indexOf(arr[i])===-1){
                    newArr.push(arr[i]);
                }
            }
            return newArr;
        }

        var demo = unique(['a','d','d','o','a','c'])
        console.log(demo)

 6. Convert array to string

1. toString() converts an array into a string

2. join('separator')

        //数组转换成字符串
        var arr4 = ['ggg','rrr','ppp'];
        console.log(arr4);
        console.log(arr4.toString());
        console.log(arr4.join());
        console.log(arr4.join('-'));
        console.log(arr4.join('$'));

 

7. Others

concat(): connects two or more arrays without affecting the original array

                returns a new array

slice(): Array interception slice(begin, end), returns a new array of intercepted items

splice(): Array deletes splice (the first number, the number of deletions), and returns a new array of deleted items, which will affect the original array

Guess you like

Origin blog.csdn.net/weixin_44400887/article/details/123989610