Home > Web Front-end > JS Tutorial > Detailed explanation of steps to use JS array method

Detailed explanation of steps to use JS array method

php中世界最好的语言
Release: 2018-05-24 16:06:22
Original
2011 people have browsed it

This time I will bring you a detailed explanation of the steps to use the JS array method. What are the precautions when using the JS array method? The following is a practical case, let’s take a look.

1.join

Function: Splice the array into a string by specifying characters .
Syntax: string arr.join([separator = ',']);
Parameters: separator can If omitted, defaults to a comma . If seprator is an empty string , then all elements in the array will be concatenated directly.
Return value: The concatenated string.

<script>
        var arr=[2,3,4,5];
        console.log(arr.join());//"2,3,4,5"
        console.log(arr.join(""));//"2345"
        console.log(arr.join("+"));//"2+3+4+5"
        console.log(arr);//[2,3,4,5]
</script>
Copy after login
2.push

Function: Add one or more elements to the end of the array.
Syntax: number arr.push(element1, ..., elementN);
Parameters: elementN, one or more elements.
Return value: The length of the new array.
Note: will modify the original array.

 <script>
        var att=[1,2,3,4];
        console.log(att.push("a","b"));//6
        console.log(att);//[1,2,3,4,"a","b"];
 </script>
Copy after login
3.unshift

Function: Add one or more elements to the head of the array.
Syntax: number arr.push(element1, ..., elementN);
Parameters: elementN, one or more elements.
Return value: The length of the new array.
Note: will modify the original array.

 <script>
        var att=[1,2,3,4];
        console.log(att.unshift("a","b"));//6
        console.log(att);//["a","b",1,2,3,4];
 </script>
Copy after login
4.pop

Function: DeletearrayLast a element.
Syntax: mixed arr.pop();
Return value: The deleted element.
Note: will modify the original array.

<script>
    var arr=[1,2,3,4];
    console.log(arr.pop());//4
    console.log(arr);//[1,2,3]
</script>
Copy after login
5.shift

Function: Delete the first element of the array. Syntax:
mixed arr.pop();Return value:
The deleted element. Note:
will modify the original array.

<script>
    var arr=[1,2,3,4];
    console.log(arr.shift());//1
    console.log(arr);//[2,3,4]
</script>
Copy after login
6.concat

Function:

Merge the incoming array or non-array value with the original array, form a new array and return . Syntax:
array array.concat(value1, value2, ..., valueN); Parameters:
valueN refers to the array or non-array value that needs to be merged with the original array.Return value:
Merged arrayNote:
will not modify the contents of the original array.

<script>
        var att=[1,2,3];
        var attO=["a","b"];
        var attT=[5,true];
        console.log(att.concat(10,attO,attT));//[1,2,3,10,"a","b",5,tru]
        console.log(att);//[1,2,3]
</script>
Copy after login
7.reverse

Function:

Reverse the position of the elements in the array.Syntax: array arr.reverse( )
Return value:
The array after reversing the orderNote:
willchange the original array.

<script>
    var arr = [1,2,3,6,5,4];
    var res = arr.reverse();
    res.push( "a" );
    console.log( res );//[4,5,6,3,2,1,"a"]
    console.log( arr );//[4,5,6,3,2,1,"a"]
</script>
Copy after login
8.slice

Function:

Intercept a part of the **array and return this new array.

Syntax:
array arr.slice([begin[,end]]);Return value:
The new array of after interceptionNote:
Contains the starting position, does not include includes the ending position, will not change the original array. If nothing is passed in, the original array will be array.

<script>
    var arr = ["a","b","c","d"];
//    var res = arr.slice( 1,3 );//["b","c"]
//    var res = arr.slice( 3,1 );//[]
    var res = arr.slice( -3,-1 );//["b","c"]
    var res = arr.slice();//["a","b","c","d"]
    console.log( res );
    console.log( arr );//["a","b","c","d"]
</script>
Copy after login
9.splice

Function:

Replace old elements with new elements to modify the contents of the array

Syntax:
array array.splice(start, deleteCount[, item1[, item2[, ...]]])Parameters:

    - start    起始位置
    - deleteCount    删除长度
    - item    添加的内容
Copy after login
Return value: An

array composed of the deleted elements Note :
modifies the contents of the original array.

<script>
    var att="Liangzhifang".split("");
    console.log(att.splice(2,4,["a"]));//["a","n","g","z"]
    console.log(att);//["L","i",["a"],"h","i","f","a","n","g"]
</script>
Copy after login

10.sort

作用: 对数组的元素进行排序。
语法: array arr.sort([compareFunction]);
参数: compareFunction可选。用来指定按某种顺序进行排列的函数。如果省略,元素按照转换为的字符串的诸个字符的Unicode位点进行排序。
返回值: 排序后的数组.

<script>
    //当什么都不传入的时候,sort()默认由小到大排列。
    var attr=[9,5,4,3,2];
    console.log(attr.sort());//[2, 3, 4, 5, 9]
    //传入function的时候,sort()内的function返回值大于0,由小到大排列。
    var attrO=[9,5,4,3,2];
    console.log(attrO.sort(function () {
        return 1;
    }));//[2, 3, 4, 5, 9]
    //传入function的时候,sort()内的function返回值小于或者等于0,数组序列不变。
    var attrT=[9,5,4,3,2];
    console.log(attrT.sort(function () {
        return -1;
    }));// [9, 5, 4, 3, 2]
    //由于sort内部是随机抽取两个值,我们在利用function函数的返回值,大于0的时候,交换位置。小于或者等于0的时候不变,来排序。
    //以下是由小到大排序
    var attrTh=[9,5,4,3,2];
    console.log(attrTh.sort(function (a,b) {
        return a-b;
    }));// [2, 3, 4, 5, 9]
    //以下是由大到小排序
    var attrF=[9,5,4,3,2];
    console.log(attrF.sort(function (a,b) {
        return b-a;// [9, 5, 4, 3, 2]
    }));
</script>
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

使用js变量作用域遇到的bug

自定义搭建前端脚手架

The above is the detailed content of Detailed explanation of steps to use JS array method. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template