0
function copy(originalFlavors) {

var originalFlavors = [
        "Banana Nut Fudge",
        "Black Walnut",
        "Burgundy Cherry",
        "Butterscotch Ribbon",
        "Cherry Macaron",
        "Chocolate",
        "Chocolate Almond",
        "Chocolate Chip",
        "Chocolate Fudge",
        "Chocolate Mint",
        "Chocolate Ribbon",
        "Coffee",
        "Coffee Candy",
        "Date Nut",
        "Eggnog",
        "French Vanilla",
        "Green Mint Stick",
        "Lemon Crisp",
        "Lemon Custard",
        "Lemon Sherbet",
        "Maple Nut",
        "Orange Sherbet",
        "Peach",
        "Peppermint Fudge Ribbon",
        "Peppermint Stick",
        "Pineapple Sherbet",
        "Raspberry Sherbet",
        "Rocky Road",
        "Strawberry",
        "Vanilla",
        "Vanilla Burnt Almond"
    ];


    console.log(copy(originalFlavors));
}
epascarello
  • 204,599
  • 20
  • 195
  • 236
codeanna
  • 1
  • 2

3 Answers3

0

function copy(arr){
  return [...arr];
}

This will return the copy of arr passed to it, you can also use this inline btw.

More about this syntax - Spread operator

EDIT: As mentioned by @Pawan Sharma in the comments, the spread operator is slower than splice, but that should only bother you if you are dealing with a huge amount of elements in the array.

Attaching recent benchmark results,

enter image description here

Nikhil Singh
  • 1,486
  • 1
  • 13
  • 24
  • Do note that spread operator is very slow as compared to arr.slice(0) – Pawan Sharma May 21 '21 at 18:53
  • @PawanSharma I agree, but it makes a considerable difference only when the array size is pretty large but here seeing the size of the array given in the question, I don't think so it is going to make any such considerable difference and also `splice` is specific to array while `spread` operator can be with used any iterable. – Nikhil Singh May 21 '21 at 18:57
  • I agree, but OP asked with respect to arrays. Also, A lot of people do not know about the performance difference in different ways to copy array. People always tend to use ```spread``` because it is newer and it looks cool. Glad to see that you pointed it out yourself. – Pawan Sharma May 21 '21 at 19:01
  • 1
    Added in the answer as well, thanks for pointing out @PawanSharma – Nikhil Singh May 21 '21 at 19:06
0
function copy(originalFlavors) {
    return [...originalFlavors];
}  

  var originalFlavors = [
                "Banana Nut Fudge",
                "Black Walnut",
                "Burgundy Cherry",
                "Butterscotch Ribbon",
                "Cherry Macaron",
                "Chocolate",
                "Chocolate Almond",
                "Chocolate Chip",
                "Chocolate Fudge",
                "Chocolate Mint",
                "Chocolate Ribbon",
                "Coffee",
                "Coffee Candy",
                "Date Nut",
                "Eggnog",
                "French Vanilla",
                "Green Mint Stick",
                "Lemon Crisp",
                "Lemon Custard",
                "Lemon Sherbet",
                "Maple Nut",
                "Orange Sherbet",
                "Peach",
                "Peppermint Fudge Ribbon",
                "Peppermint Stick",
                "Pineapple Sherbet",
                "Raspberry Sherbet",
                "Rocky Road",
                "Strawberry",
                "Vanilla",
                "Vanilla Burnt Almond"
            ];  
    console.log(copy(originalFlavors));
0

Use slice to return a new array and not a reference for the received array.

function copy(originalFlavors) {
  return originalFlavors.slice()
}

// or with ES6 syntax

const copy = originalArray => originalArray.slice()
Pablo Darde
  • 5,844
  • 10
  • 37
  • 55