# 利用 reduce 方法对数据进行归并组合
const tyreList = [
{
type: "02",
},
{
type: "04",
},
{
type: "05",
},
{
type: "06",
},
];
const arr = [2, 4];
let cacheCount = 1;
const resultArr = arr.reduce((acc, item) => {
const tempArr = new Array(item).fill({});
const median = tempArr.length / 2;
const leftTyreArr = [];
const rightTyreArr = [];
tempArr.forEach((t, index) => {
const newInfo = { type: `0${cacheCount}` };
const f = tyreList.find((l) => l.type === `0${cacheCount}`);
if (!f) newInfo.className = "no-info-tyre";
cacheCount += 1;
if (index < median) {
leftTyreArr.push(newInfo);
} else {
rightTyreArr.push(newInfo);
}
});
acc.push.call(acc, {
leftTyreArr,
rightTyreArr,
tyreType: tempArr.length > 2 ? "back" : "front",
});
return acc;
}, []);
console.log("result", resultArr);
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
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