Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dinamyc psuh to array by length

I stuck with my code.

I have an array like this:

let length = 2;
let data1 = ['Label1', 'Label2', 'Label3'];
let data2 = []

I want to push the data2 with data1 value by length. The expectation I want is like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data2 = ['Label1', 'Label2', 'Label3', 'Label1', 'Label2', 'Label3'];

anyone can help? thanks

>Solution :

You can easily achieve the result using Array.from and flat

let length = 2;
let data1 = ["Label1", "Label2", "Label3"];
let data2 = [];

data2 = Array.from({ length }, () => data1).flat();
console.log(data2);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading