Home > Web Front-end > JS Tutorial > body text

Detailed explanation of basic methods of arrays in js

小云云
Release: 2018-03-13 15:17:54
Original
984 people have browsed it


This article mainly shares with you a detailed explanation of the basic methods of arrays in js. I hope it can help you.

1. arr.join( )

    //join 数组==>字符串,该字符串以特定字符分隔开.
    var arr = ['A','B','C'];
    var str = arr.join('|');
    console.log(str);
    console.log(typeof str);
    console.log(arr);
    console.log(typeof arr);//object
Copy after login

2. arr.push( ) <==> arr.pop( )

    var arr = ['A','B','C'];
    //push添加元素,在数组之后添加元素,返回的是新数组的长度
    var count = arr.push('D','E');
    console.log(count);
    console.log(arr);
    //pop()删除数组的最后一元素并且返回该元素,数组长度减去1
    var count = arr.pop();
    console.log(count);
    console.log(arr);
Copy after login

3. arr.unshift ( ) <==> arr.shift( )

    var arr = ['A','B','C'];
    //shift()删除数组第一个元素,并且返回该元素
    console.log(arr.shift());
    console.log(arr);

    //unshift()添加元素,在数组之前添加,返回的是新数组的长度
    console.log(arr.unshift('D','E'));
    console.log(arr);
Copy after login

4. arr.reverse( )

    //reverse()倒序数组,返回的是倒序之后的数组,原数组的顺序改变
    var arr = ['A','B','C'];
    console.log(arr.reverse());
    console.log(arr);
Copy after login
    //concat()连接数组并且返回,连接后的类型为数组
    var arr = ['A','B','C'];
    var copyArr = arr.concat(11,'F',["123",'4345']);
    console.log(copyArr);
    console.log(arr);
    copyArr.push('r','556');
    console.log(copyArr);
Copy after login
Copy after login

5. arr.concat( )

    //concat()连接数组并且返回,连接后的类型为数组
    var arr = ['A','B','C'];
    var copyArr = arr.concat(11,'F',["123",'4345']);
    console.log(copyArr);
    console.log(arr);
    copyArr.push('r','556');
    console.log(copyArr);
Copy after login
Copy after login

6. indexOf( ) <==> lastIndexOf( )

    //indexOf()
        //一个参数返回的是数组中的下标
        //第二个参数表示:从什么位置开始查找.如果找到返回的是该元素在数组中的下标,找不到则返回-1.

    var arr = [1,5,9,32,74,36,8];
    console.log(arr.indexOf(5,1));

    //lastIndexOf()
        //一个参数:表示需要查询的元素,返回值是该元素的下标
        //两个参数:第一个表示需要查询的元素,第二个表示从什么位置开始查询;
                //该方法查询方向是反向,但是返回值仍按照正序排列
    var arr1 = [1,5,9,32,74,36,8];
    console.log(arr1.lastIndexOf(37));
Copy after login

Summary:

    // push();向数组最后位置添加元素,返回的是数组的长度
    // pop();删除数组最后一个元素,返回的是删除的该元素
    // unshift();向数组的最前端添加元素,返回的是数组的长度
    // shift();删除数组第一个元素,返回的是删除的该元素

    //join();把数组各个元素拼接为字符串,并且以特定的字符分隔开,
           //返回值是带有特殊字符分隔的字符串

    //reverse()倒序数组
           //返回的是倒序之后的数组

    //concat();拼接数组,返回拼接之后的数组

    //indexOf();执行查询操作
           //如果只有一个参数, 则查询该元素; 找到的话, 返回该元素的下标, 找不到的话, 返回-1.
           //如果含有两个参数, 则: 第一个参数表示查询的目标元素
           //                  第二个参数表示正序从‘参数二’开始查找
    //lastIndexOf();执行查询操作
           //如果只有一个参数, 则查询该元素; 找到的话, 返回该元素的下标, 找不到的话, 返回-1.
           //如果含有两个参数, 则: 第一个参数表示查询的目标元素
           //                   第二个元素表示反序从‘参数二’开始查找
Copy after login


Related recommendations:

Six types of JS array deduplication Method sharing

Detailed explanation of js array deduplication and deflattening

The most practical JS array function organization

The above is the detailed content of Detailed explanation of basic methods of arrays in js. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!