-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy path05 Replacing an Array.html
87 lines (81 loc) · 2.43 KB
/
05 Replacing an Array.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="vue.js"></script>
<style>
.blue {
color: blue;
}
</style>
</head>
<body>
<div id="example-1">
<ul>
<template v-for="item in items">
<li>{{$index}}.{{ item.msg }}</li>
</template>
</ul>
<button v-on:click="f1">执行 f1 filter /Foo/ </button>
<button v-on:click="f2">执行 f2 concat arr </button>
<button v-on:click="f3">执行 f3 splice(2,3) </button>
<div class="blue">
{{$data | json }}
</div>
<pre>
变异方法,如名字所示,修改了原始数组。相比之下,也有非变异方法,如 filter(), concat() 和 slice(),不会修改原始数组而是返回一个新数组。
在使用非变异方法时,可以直接用新数组替换旧数组:
可能你觉得这将导致 Vue.js 弃用已有 DOM 并重新渲染整个列表——幸运的是并非如此。
Vue.js 实现了一些启发算法,以最大化复用 DOM 元素,
因而用另一个数组替换数组是一个非常高效的操作。
</pre>
</div>
<script>
var example1 = new Vue({
el: '#example-1',
data: {
items: [{
msg: 'Foo'
}, {
msg: 'Bar'
}, {
msg: 'George'
}, {
msg: 'John'
}, {
msg: 'Thomas'
}, {
msg: 'James'
}, {
msg: 'Adrew'
}, {
msg: 'Martin'
}],
arr: [{
msg: '台湾小凡'
}, {
msg: '感谢'
}, {
msg: '群友'
}]
},
methods: {
f1: function() {
example1.items = example1.items.filter(function(item) {
return item.msg.match(/Foo/)
})
},
f2: function() {
example1.items = example1.items.concat(this.arr[0])
example1.items = example1.items.concat(this.arr[1])
example1.items = example1.items.concat(this.arr[2])
},
f3:function (){
example1.items = example1.items.splice(2,3)
}
}
})
</script>
</body>
</html>