clone array functions and objects

Asked

Viewed 47 times

-3

Make a function to clone arrays. (Tip: clone = [ ...clone ])
Make a function to clone objects. (Tip: clone = { ...objeto })

let total = 3, array1 = [1,2,3]


const clonar = () => {
     if (total === 0){
         console.log('clonado');
     }
}

console.log()
  • What is your difficulty?

  • how to think logic, not being able to clone

  • 2

    The hint is no longer the answer to the question itself?

1 answer

1


The ideal would be to use the Operator spread (...):

    var array1 = [1,2,3]
    var clone = [...array1]

The function that clones could be like this:

    function clonaArray(array){
     var clone = [...array];
     return clone;
    }

    console.log(clonaArray(array1))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.