-
Notifications
You must be signed in to change notification settings - Fork 5
/
10数组遍历方法.html
50 lines (45 loc) · 1.1 KB
/
10数组遍历方法.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>数组遍历方法</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h2>{{name}}</h2>
<h2>{{arr}}</h2>
<h2>{{arr2}}</h2>
<button @click='dian'>点击</button>
</div>
</body>
<script>
var app = new Vue({
el: '#app',
data: {
name: '小明',
arr: [1, 2, 3, 3, 2, 1, 5],
arr2: []
},
methods: {
dian() {
// forEach方法单纯遍历
// this.arr.forEach(item => {
// console.log(item+1);
// });
// map方法遍历数组元素,可以操作元素,返回新数组
// this.arr2=this.arr.map(item=>{
// console.log(item);
// return item+1
// })
// filter方法,遍历筛选每一个数组,返回筛选元素后的数组
this.arr2=this.arr.filter(item=>{
console.log(item);
return item>2
})
}
}
})
</script>
</html>