用js 把他变成四层数组
时间: 2023-08-10 22:15:02 浏览: 43
好的,以下是将该 JSON 数据转换为四层数组的 JavaScript 代码:
```javascript
const data = [
{
"branch_id": 5,
"branch_name": "分院2",
"major_id": 3,
"major_name": "专业2",
"class_id": 5,
"class_name": "班级2",
"student_id": 11,
"name": "xs1"
},
{
"branch_id": 5,
"branch_name": "分院2",
"major_id": 3,
"major_name": "专业2",
"class_id": 5,
"class_name": "班级2",
"student_id": 10,
"name": "xs3"
},
{
"branch_id": 5,
"branch_name": "分院2",
"major_id": 3,
"major_name": "专业2",
"class_id": 5,
"class_name": "班级2",
"student_id": 9,
"name": "bj2xs3"
},
{
"branch_id": 5,
"branch_name": "分院2",
"major_id": 3,
"major_name": "专业2",
"class_id": 5,
"class_name": "班级2",
"student_id": 8,
"name": "bj2xs2"
}
];
const result = data.reduce((acc, cur) => {
const branchIndex = acc.findIndex(item => item.branch_id === cur.branch_id);
if (branchIndex === -1) {
acc.push({
branch_id: cur.branch_id,
branch_name: cur.branch_name,
majors: [
{
major_id: cur.major_id,
major_name: cur.major_name,
classes: [
{
class_id: cur.class_id,
class_name: cur.class_name,
students: [
{
student_id: cur.student_id,
name: cur.name
}
]
}
]
}
]
});
} else {
const majorIndex = acc[branchIndex].majors.findIndex(item => item.major_id === cur.major_id);
if (majorIndex === -1) {
acc[branchIndex].majors.push({
major_id: cur.major_id,
major_name: cur.major_name,
classes: [
{
class_id: cur.class_id,
class_name: cur.class_name,
students: [
{
student_id: cur.student_id,
name: cur.name
}
]
}
]
});
} else {
const classIndex = acc[branchIndex].majors[majorIndex].classes.findIndex(item => item.class_id === cur.class_id);
if (classIndex === -1) {
acc[branchIndex].majors[majorIndex].classes.push({
class_id: cur.class_id,
class_name: cur.class_name,
students: [
{
student_id: cur.student_id,
name: cur.name
}
]
});
} else {
acc[branchIndex].majors[majorIndex].classes[classIndex].students.push({
student_id: cur.student_id,
name: cur.name
});
}
}
}
return acc;
}, []);
console.log(result);
```
运行结果如下:
```
[
{
"branch_id": 5,
"branch_name": "分院2",
"majors": [
{
"major_id": 3,
"major_name": "专业2",
"classes": [
{
"class_id": 5,
"class_name": "班级2",
"students": [
{
"student_id": 11,
"name": "xs1"
},
{
"student_id": 10,
"name": "xs3"
},
{
"student_id": 9,
"name": "bj2xs3"
},
{
"student_id": 8,
"name": "bj2xs2"
}
]
}
]
}
]
}
]
```
以上代码使用了 JavaScript 的 `reduce` 函数来将数据转换为四层数组,并且使用了 `findIndex` 函数来判断数组中是否已经存在相应的元素。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)