dorkahemp972157683 2018-09-09 14:10
浏览 150
已采纳

Javascript自动增量数组

Is there a shorthand way to auto increment a Javascript array like you can in PHP?

PHP Example:

$myArray=[];
$myArray[] = [ 'item1' , 'item2' ];
$myArray[] = [ 'item3' , 'item4' ];

JS Example:

let myArray = [];
myArray[ myArray.length ] = [ 'item1' , 'item2' ];
myArray[ myArray.length ] = [ 'item3' , 'item4 '];
//or
myArray.push( [ 'item1' , 'item2' ] );
myArray.push( [ 'item3' , 'item4' ] );

Without using myArray.length or myArray.push()

  • 写回答

3条回答 默认 最新

  • doufu8588 2018-09-09 14:47
    关注

    Here is the ES6 way, using spread operator

    const arr1 = [1,2,3];
    const arr2 = [3,4,5];
    const arr3 = [...arr1, ...arr2]; // arr3 ==> [1,2,3,3,4,5]
    

    OR

    just by using the concat method

    const arr1 = [1,2,3];
    const arr2 = [3,4,5];
    const arr3 = arr1.concat(arr2); // arr3 ==> [1,2,3,3,4,5]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员