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方法来使用。

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-28
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2021-08-14
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2018-11-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案