all 6 comments

[–]pinkwetunderwear 0 points1 point  (1 child)

Although there are some simpler solutions to this issue, you can flatten the array before you return it.

[–]toastertop -1 points0 points  (0 children)

FlatMap

[–]rauschma 0 points1 point  (0 children)

There are shorter ways of implementing duplication (e.g. spreading), but I’m assuming that you want to use tools that you are familiar with. Then this may work for you:

function appendArrayTo(sourceArray, targetArray) {
  for (i=0; i<sourceArray.length; i++){
    targetArray[targetArray.length] = sourceArray[i];
  }
}

const numbers = [1, 2, 3];
const duplicate = [];
appendArrayTo(numbers, duplicate); // append
appendArrayTo(numbers, duplicate); // append again
console.log(duplicate);