zoukankan      html  css  js  c++  java
  • js数组操作

    一、数组常用方法:

    1、创建

    1)构造函数

    var a = new Array(); //无参数构造,返回 []
    var b = new Array(5); //一个参数表示数组长度,返回 [undefined × 5],b.length == 5
    var c = new Array(1,2,3); //多个参数为初始化数组,返回 [1, 2, 3]

    2)字面量

    var a = [];
    var b = [1,2,3];
    var c = [1,2,3,]; //length在现代浏览器是3,低版本ie下数组长度可能为4

    2、访问

    以下标索引的方式获取值或赋值

    var a = [1,2,3];
    console.log(a[0]); //返回1
    var index = 1;
    console.log(a[index]); //返回2,索引可以是变量

    3、添加,push、unshift、splice

    var a = [1];
    a.push(2); //在数组的尾部添加
    a.unshift(3); //在数组的头部添加
    a.splice(1, 0, 4,5,6);  //在索引为1的位置处开始插入4,5,6
    console.log(a); //[3, 4, 5, 6, 1, 2]

    4、删除,pop、shift、splice,都有返回值,返回的是被删除的元素,splice返回的是被删除的数组

    var a = [1, 2, 3, 4, 5, 6];
    a.pop(); //删除数组的尾部一个元素
    a.shift(); //删除数组的头部一个元素
    a.splice(1, 3);  //在索引为1的位置处开始删除3个元素
    console.log(a); //[2]

    ======================

    以上3到4的函数都是对数组本身做了更改

    ======================

    5、截取,slice

    复制代码
    var a = [1, 2, 3, 4, 5, 6];
    var b = a.slice(2);
    console.log(a); //[1, 2, 3, 4, 5, 6]
    console.log(b); //[3, 4, 5, 6]
    var c = a.slice(2,4);
    console.log(a); // [1, 2, 3, 4, 5, 6]
    console.log(c); // [3, 4]
    复制代码

    6、合并和拷贝,concat

    1)合并

    复制代码
    var a = [];
    var b = [1,2,3];
    var c = [1,2,3,];
    a.concat(b, c);
    console.log(a); // 返回空数组[]
    var d = a.concat(b, c);
    console.log(d); //返回[1, 2, 3, 1, 2, 3]
    复制代码

    2)拷贝

    var a = [1,2,3];
    var b = a.concat(); //不加参数是拷贝数组
    console.log(b); //[1,2,3]

    ======================

    以上5到6的函数都不对数组本身做更改,而是返回一个新数组

    ======================

    7、排序,reverse,sort

    var a = [1,2,3];
    a.reverse(); //反序数组
    console.log(a); //[3, 2, 1]

    sort可以传入排序函数,数组中的对象可以根据某个键的值来排序

    复制代码
    var a = [{
        id: 1,
        name: 'lsy'
    },{
        id:10,
        name: 'lsy10'
    },
    {
        id:3,
        name: 'lsy3'
    },
    {
        id:9,
        name: 'lsy9'
    }
    ];
    a.sort(function (b,c) {
    return b.id-c.id
    });
    console.log(JSON.stringify(a, null, 4));
    
     [
        {
            "id": 1,
            "name": "lsy"
        },
        {
            "id": 3,
            "name": "lsy3"
        },
        {
            "id": 9,
            "name": "lsy9"
        },
        {
            "id": 10,
            "name": "lsy10"
        }
    ]
    复制代码

    8、字符串化,join,把数组的元素用符合连接成字符串

    var a = [1,2,3];
    var str = a.join(','); //将数组用逗号连接
    console.log(a); //[1, 2, 3]
    console.log(str); //1,2,3
  • 相关阅读:
    [基础架构]PeopleSoft都有哪些进程运行在进程服务器上
    [基础架构]PeopleSoft Process Scheduler 重要文件说明
    .NET Core微服务之基于Consul实现服务治理
    .net core3.1 Filter四种注入方式和异常过滤器
    ASP.NET Core配置监听URLs的六种方式
    Asp.Net Core中JWT刷新Token解决方案
    ASP.NET Core-ActionFilter实现依赖注入(ServiceFilterAttribute 、TypeFilterAttribute) 【转】
    asp.net core 3.1配置log4net
    使用 Certbot 安装 Letsencrypt 证书
    使用新版 winsw 注册 windows 系统服务无法启动及停止问题
  • 原文地址:https://www.cnblogs.com/snowhite/p/9134678.html
Copyright © 2011-2022 走看看