美文网首页
JavaScript数组方法Array.prototype.co

JavaScript数组方法Array.prototype.co

作者: 游海东 | 来源:发表于2019-12-20 14:40 被阅读0次

    1. 基本语法

    新数组 = 老数组.concat(值1[, 值2[, ...[, 值N]]])

    • 作用:用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。
    • 参数:值可以使用基本数据类型的,也可以是数组,还可以是两者组合起来的

    2. 基本实例

    2.1 合并两个数组

    let a = [1,2,3,4,5,6];
    let b = ['a','b','c','d','e'];
    let c = a.concat(b);
    console.log(a);
    console.log(b);
    console.log(c);
    

    结果:
    [1, 2, 3, 4, 5, 6]
    ["a", "b", "c", "d", "e"]
    [1, 2, 3, 4, 5, 6, "a", "b", "c", "d", "e"]

    2.2 合并三个数组

    let a = [1,2,3,4,5,6];
    let b = ['a','b','c','d','e'];
    let c = ['1',2,'e','3','t'];
    let d = a.concat(b,c);
    console.log(a);
    console.log(b);
    console.log(c);
    console.log(d);
    

    结果:
    [1, 2, 3, 4, 5, 6]
    ["a", "b", "c", "d", "e"]
    ["1", 2, "e", "3", "t"]
    [1, 2, 3, 4, 5, 6, "a", "b", "c", "d", "e", "1", 2, "e", "3", "t"]

    2.3 数值和数组

    let a = [1,2,3,4,5,6];
    let b = 100;
    let c = ['1',2];
    let d = a.concat(b,c);
    console.log(a);
    console.log(b);
    console.log(c);
    console.log(d);
    

    结果:
    [1, 2, 3, 4, 5, 6]
    100
    ["1",2]
    [1, 2, 3, 4, 5, 6, 100, "1", 2]

    2.4 嵌套数组

    let a = [1,2,3,4,5,6];
    let b = 100;
    let c = ['1',['a','b','c',1,4,5]];
    let d = [[[1,2,3,4],[2,3,4,5,6],2,4,5],7,8,9]
    let e = a.concat(b,c,d);
    console.log(a);
    console.log(b);
    console.log(c);
    console.log(d);
    console.log(e);
    
    实例结果

    相关文章

      网友评论

          本文标题:JavaScript数组方法Array.prototype.co

          本文链接:https://www.haomeiwen.com/subject/syqinctx.html