首页 > 解决方案 > 如何在javascript中复制数组值

问题描述

输入
var key=[ 'id', 'name', 'health', 'status', 'environment', 'serviceType' ]

我期待以下输出(值的重复)

['id','name','health','status','environment','serviceType','id','name','health','status','environment','serviceType']

标签: arrays

解决方案


使用spread-operator

const key = [ 'id', 'name', 'health', 'status', 'environment', 'serviceType' ]

const res = [...key, ...key];

console.log(res);

使用Array#concat

const key = [ 'id', 'name', 'health', 'status', 'environment', 'serviceType' ]

const res = key.concat(key);

console.log(res);


推荐阅读