1. 程式人生 > >vue.js 陣列更新

vue.js 陣列更新

改變原始陣列

push()

pop()

shift()

unshift()

splice()

sort()

reverse()

不改變原陣列

filter()

contat()

slice()

例項:

computed:{
       filterBooks(){
            return this.books.filter(function(book){
                return book.name.match(/javascript/)
            })
        },
        sortBooks(){
            return this.lists.sort(function (a,b) {
                return a.name.length<b.name.length
            })
        }
}
mounted(){
   this.books.push({              //push陣列方法,檢視直接改變
            name:'《css3實戰》',
            author:'huiyh'
        });
        this.books=this.books.concat([{  //concat陣列方法,需更新原陣列
            name:'《html5實戰》',
            author:'lala'
        }])
}