Home  >  Article  >  Web Front-end  >  Detailed explanation of built-in object Array in JS

Detailed explanation of built-in object Array in JS

小云云
小云云Original
2018-03-29 17:34:481475browse

This article mainly shares with you a detailed explanation of the built-in object Array in JS. The built-in objects of JS are mainly divided into four categories: Array String Math Date. We mainly introduce Array, hoping to help everyone.

Array

Each item in the array can save any type of data, and the size of the array can be adjusted as needed.

1. Create

a. Use the constructor:

var colors= new Array( ) The brackets can be the number of data in the array, or each item Specific data, but it should be noted that each item of data is separated by commas.

b. Array literal representation

var colors=[1,2,3] Separate each item of data with commas.

2. The access and setting of the element

[] starts from 0. If the length of the array is exceeded, the return value is undefined.

3. Length

          array.length, the return value is number.

                                                                                                      ’ ’ s Str               through ’ ’ ’s ’ ’s way through ’ through through through array.length ’ s   through ’ through ’ through ’ ’s ’ through through ‐ through ‐‐ ‐‐ ‐ _ _ _ array.length, the return value is number.

# When placing a position beyond the size of the current array, the system will automatically re -calculate the length of the array, and the length value is added to the last index.

4. Traversal

for(var i=0;i<array.length;i++) {
  console.log(array[i]);
}

5. Some commonly used methods in arrays

a. push( ) arrayObject.push( value1,value2...)

                                                                                          ‐                                                                                                                                                      ... value2 ...)

Add the value in the brackets in order to ArrayObject head

rebate the new length of the array


C. POP ( ) arrayObject.pop( ) Delete the last element and return the deleted element


d. shift( ) arrayObject.shift( ) Delete the first element and return the deleted element


e. join( ) arrayObject.join(separator)

Put the elements in the array into a string and return a string; the separator defaults to comma, and the default space is not written.

f. sort( ) arrayObject.sort( )

Sorts the array elements, and the return value is the array

Even if each in the group The items are all numbers. The comparison of this method is still a string. Sort in the order of the string


# This can receive a comparison function as a parameter


Array.sort (function (x, y) {

                                                                                                                                                      through

                                                        return y-x;  //  降序输出

                                                                            })

    g. reverse( )             arrayObject.reverse( )   将数组中的元素颠倒然后输出,返回数组

    h. concat( )                arrayObject.concat(数组1,数组2...... )  连接两个或者多个数组,返回数组

                                       arr3=arr1.concat(arr2)    1连接2然后放到3里面

    i. slice( )                   arrayObject.slice( start ,end )    返回数组选定的元素  返回值为数组

                                start:必需,从哪开始,可以是负数,负数+数组的长度就是start

                                end:可选,截止位置,不写默认到最后一个字符

                                截取的元素从start开始,到end-1结束

                              [例题]:var a=[1,'yes',3] b; 请做b对a的复制,方法越多越好

                                方法一:数组遍历

                                      

b=new Array();
for( var i=0;i<a.length;i++)
{
  b.push(a[i]);
}

                                   方法二: 使用concat( )

b=[].concat(a);

                                    方法三:使用slice( )

b=a.slice(0);

    j.  splice( ) 

       f35d6e602fd7d0f0edfa6f7d103c1b57 删除   arrayObject.splice( index,count)

        删除从index处开始的count个元素,返回被删除元素的数组,count为0时,不做任何操做,count不设置值时,从index后的所有元素都将被删除。

       2cc198a1d5eb0d3eb508d858c9f5cbdb 插入   arrayObject.splice( index,0,value1,value2......)

        从index位置插入value的值,返回数组。在索引的后一位插入数据

      5bdf4c78156c7953567bb5a0aef2fc53 替换   arrayObject.splice( index,count,value1,value2......)

        返回值:从原始数组中删除的数据,没删除返回空数组,     count为要删除的项数

    k. indexOf( )           arrayObject.indexOf( searchvalue, startIndex)

                          searchvalue:必需,所要查找的数据

Startindex: Optional, starting point, the default is 0

This return value: Number, the item of the back search is in the array position, no return-1


## l. lastIndexOf( ) is similar to the previous one, starting from the last element of the array.

Related recommendations:

php custom two-dimensional array sorting function array

###

The above is the detailed content of Detailed explanation of built-in object Array in JS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn