1
I would like to clone a list of numbers and be able to manipulate that cloned list without changing the source, but in the following situation when I delete the item from the cloned array it deletes the source.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<ul>
<h3>Lista Origem</h3>
<li v-for="item in listaOriginal">
{{item}}
</li>
</ul>
<ul>
<h3>Lista Clone</h3>
<li v-for="item in listaClone">
{{item}}
</li>
</ul>
<button @click="clonaLista">Clonar Lista</button>
<button @click="removerItem">Remover item do Clone</button>
</div>
<script>
const vue = new Vue({
el: "#app",
data: {
listaOriginal: [1, 2, 3],
listaClone: []
},
methods: {
clonaLista() {
this.listaClone = this.listaOriginal
},
removerItem() {
this.listaClone.pop()
}
}
})
</script>
</body>
</html>
That way has already been mentioned in my reply. What it does different from what I mentioned?
– fernandosavio