欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JS数组的查找、截取和拼接

程序员文章站 2023-12-22 22:36:10
...
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组的查找、截取和拼接</title>
</head>
<body>
    <script>
        /*
            indexOf(searchValue[,fromIndex])
                查找目标字符串中,是否包含searchValue,如果包含,返回第一次出现的索引,如果不包含,返回-1
                - searchValue 要查找的值
                - fromIndex 查找的起始位置,如果不填,默认为0
                  如果大于或者等于length,则返回-1
                  如果说是一个负数(如果这个负数的绝对值超过length,则fromIndex为0),根据倒数来确认查找的位置
                  length + fromIndex
        */
        var arr1 = ["E","a","b","c","a","d"];
        console.log(arr1.indexOf("vv"));    //打印:-1
        console.log(arr1.indexOf("a",arr1.length));    //打印:-1
        console.log(arr1.indexOf("a", -2));    //打印:4
        console.log(arr1.indexOf("a", -50));    //打印:1




        /*
            lastIndexOf(searchValue[,fromIndex])
                返回数组中指定的值最后一次出现的位置,如果不存在,则返回-1
                fromIndex 
                    搜索的起始位置,默认为length
                    如果大于或者等于length,则为length
                    如果是一个负数,则倒数找到对应的起始位置
                    length - fromIndex
                    如果负数的绝对值大于length,则返回 -1

        */
        console.log(arr1.lastIndexOf("a", arr1.length - 3));    //打印:1
        console.log(arr1.lastIndexOf("a", 50));    //打印:4
        console.log(arr1.lastIndexOf("vv"));    //打印:-1




        /*
            slice(begin,end)
                用于截取数组中的内容,返回一个新数组
                - begin
                    - 如果不给,默认为0
                    - 截取的起始位置
                    - 如果值超过了length,就会返回一个空数组给你
                    - 如果是一个负数(负数的绝对值超过了length,就会默认从0开始截取),那就说明从倒数第begin开始截取
                        length + begin
                - end
                    - 如果不给,默认到数组结尾
                    - 控制截取的结束位置
                    - 如果小于(从位置上来说)bengin,会得到一个空字符串
                    - 如果是负数(如果负数的绝对值超过了length,那么就会得到一个空字符串),那么就说明从倒数第end个结束。
                    - 如果超过了length,默认也只到数组的结尾位置
                begin是包含在内的,end是不包含在截取范围内
        */
        var arr2 = ["a","b","c","d","e"];
        console.log(arr2.slice(2));    //打印:["c","d","e"]
        console.log(arr2);    //打印:["a","b","c","d","e"]
        console.log(arr2.slice(50));    //打印:[]
        console.log(arr2.slice(-2));    //打印:["d","e"]
        console.log(arr2.slice(-50));    //打印:["a","b","c","d","e"]
        console.log(arr2.slice(2,-1));    //打印:["c","d"]
        console.log(arr2.slice(0, -50));    //打印:[]
        console.log(arr2.slice(0, 50));    //打印:["a","b","c","d","e"]

        /*
            深拷贝:
                数据拷贝过来后,和原数组之间完全没有任何关系了
            浅拷贝:
                数据拷贝过来后,里面的第一层的基本类型是没有关联了,但是里面的复杂类型,依旧是有关联
            赋址:
                数据不管是基本类型,还是复杂类型,都和原来的是相关联的
        */
        //赋址
        arr3 = arr2
        arr3[0] = "123";
        console.log(arr2,arr3);    //打印:["123","b","c","d","e"] ["123","b","c","d","e"]

        var arr4 = ["a","b","c","d","e"];
        var arr5 = arr4.slice();
        arr5[0] = "123";
        console.log(arr4,arr5);    //打印:["a","b","c","d","e"] ["123","b","c","d","e"]

        var arr6 = [[1,2,3],"a"];
        var arr7 = arr6.slice();
        arr6[0][0] = "good";
        console.log(arr6,arr7);    //打印:[["good",2,3],"a"] [["good",2,3],"a"]




        /*
            concat 用于拼接两个或两个以上的数组,并返回一个新数组,并不会改变原有数组的值
        */
        var arr8 = ["I"];
        var arr9 = ["love"];
        var arr10 = ["you!"];
        console.log(arr8.concat(arr9,arr10));    //打印:["I", "love", "you!"]




        /*
            join(sparator)
                把数组拼接起来,以字符串的方式,进行返回,如果不给参数,默认以逗号进行间隔
                sparator
                    决定以什么符号对数组的里面进行拼接
                ** 如果数组是一个空数组的话,则会返回一个空字符串

        */
        var arr11 = ["I", "love", "you!"]
        console.log(arr11.join(" "));    //打印:I love you!
        var arr12 = [""];
        console.log(arr12.join());    //打印:空
        
    </script>
</body>
</html>

 

上一篇:

下一篇: