user7400978
user7400978

Reputation:

how to duplicate the array values in javascript

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

I am expecting the below output(duplication of the values)

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

Upvotes: 1

Views: 28

Answers (2)

ibragim
ibragim

Reputation: 5

Try to make with for as a solution

for(let i=0; i<key.length; i++){
   key.push(key);
}

Upvotes: 0

Majed Badawi
Majed Badawi

Reputation: 28414

Using the spread-operator:

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

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

console.log(res);

Using Array#concat:

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

const res = key.concat(key);

console.log(res);

Upvotes: 1

Related Questions