优化从两个数组中循环判断相等取值操作
data() {
return {
checkBoxList: [
{
label: '1',
name: 'one'
},
{
label: '2',
name: 'two'
},
{
label: '3',
name: 'three'
}
],
checkListTwo: [
'one', 'two', 'three'
]
}
}
// for循环
const names = []
for (let i = 0; i < this.checkBoxList.length; i++) {
for (let j = 0; j < this.checkListTwo.length; j++) {
if (this.checkBoxList[i].name == this.checkListTwo[j]) {
names.push(this.checkBoxList[i].name)
}
}
}
console.log('names ==> ', names); // ["one", "two", "three"]
// 优化
const names1 = this.checkBoxList.filter(item => this.checkListTwo.includes(item.name)).map(item =>item.name)
console.log('names1 ==> ', names1); // ["one", "two", "three"]
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
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
在线编辑 (opens new window)
上次更新: 2021/11/14, 07:48:46