2

I want to duplicate array with same elements that contains the first array For example, I have this array

let array1 = [{title: "test1"}, {title: "Test2"}, {title: "test3"}];

I need to transform it into

array1 = [{title: "test1"}, {title: "Test2"}, {title: "test3"}, {title: "test1"}, {title: "Test2"}, {title: "test3"}];

Thanks a lot.

2

3 Answers 3

4

try to concat, like:

let array1 = [{title: "test1"}, {title: "Test2"}, {title: "test3"}];

array1 = array1.concat(array1);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, you helped me a lot <3
1

I am not sure what you are trying to achieve, but here's a solution.

let array1 = [{title: "test1"}, {title: "Test2"}, {title: "test3"}];  
array1 = [ ...array1, ...array1]

1 Comment

Thanks a lot, you helped me a lot <3
-2

You can also achieve it with a little for loop :)

let arr = [1, 2, 3] ;
let new_array = [] ;
for(let i = 0 ; i < 2 ; i++) {
    new_array = new_array.concat(arr) ;
}

console.log(new_array) ;

1 Comment

Maybe because you need to replace 3 by the expected repetition ? ... Maybe you should take a better look A_A

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.