vue中肯定遇到過這個問題,想對數組操作,可是原數組又會改變,怎么辦呢,提前copy一份能行嗎,
todo: [1,2,3,4,5],
var arr = this.todo;
這樣肯定不行的,那么肯定是新建數組循環添加,把老數組的元素全部添加到新數組里。
var newArr = [];
todo.forEach(function(e,i){
newArr.push(e);
})
然后拿到新數組去操作,有點麻煩,就可以用到計算屬性。
computed:{
changeTodo: function(){
return this.todo.filter(function(number){
return number%2 ===0
})
}
}
這樣就得到了新數組也不會改變老數組的內容。
計算屬性不適用的時候可以換成methods方法來使用。
