41
rated 0 times
[
47]
[
6]
/ answers: 1 / hits: 27524
/ 6 Years ago, wed, february 28, 2018, 12:00:00
I'm working with js object, lets say:
items: [{text: 'text1', active: true},
{text: 'text1', active: true},
{text: 'text1', active: true}]
I want to make the copy of the object and make some changes with them in computed property like this way:
computed: {
copyAndChange() {
var itemsCopy = []
itemsCopy = this.items
for (var i=0; i<itemsCopy.length; i++) {
itemsCopy[i].text = something
console.log('text from items: ' + this.item[i])
console.log('text from itemsCopy: ' + itemsCopy[i])
}
return itemsCopy
}
}
This code gives me in console:
text from items: something
text from itemsCopy: something
text from items: something
text from itemsCopy: something
text from items: something
text from itemsCopy: something
(?) why? I expected:
This code gives me in console:
text from items: text1
text from itemsCopy: something
text from items: text1
text from itemsCopy: something
text from items: text1
text from itemsCopy: something
what is wrong here?
More From » vue.js