[
{name: '01', week: '周二', classes: '1-2', info: '数学'},
{name: '01', week: '周三', classes: '3-4', info: '语文'},
{name: '01', week: '周四', classes: '3-4', info: '体育'},
{name: '01', week: '周五', classes: '7-8', info: '英语'},
{name: '02', week: '周二', classes: '3-4', info: '语文'},
{name: '02', week: '周三', classes: '5-6', info: '数学'},
{name: '02', week: '周四', classes: '3-4', info: '政治'},
{name: '02', week: '周五', classes: '7-8', info: '英语'},
]
[
{
name: '01',
info: [
{
week: '周二',
classes: '1-2',
class_info: '数学'
},
{
week: '周三',
classes: '3-4',
class_info: '语文'
},
{
week: '周四',
classes: '3-4',
class_info: '体育'
},
{
week: '周五',
classes: '7-8',
class_info: '英语'
}
]
},
{
name: '02',
info: [
{
week: '周二',
classes: '3-4',
class_info: '语文'
},
{
week: '周三',
classes: '5-6',
class_info: '数学'
},
{
week: '周四',
classes: '3-4',
class_info: '政治'
},
{
week: '周五',
classes: '7-8',
class_info: '英语'
}
]
}
]
求教该如何使用 js 转换?
1
zhixiao 2022-06-06 17:11:14 +08:00
用 lodash 的 groupBy 转一下就行了
|
2
rabbbit 2022-06-06 17:21:43 +08:00
const fooList = [
{ name: "01", week: "周二", classes: "1-2", info: "数学" }, { name: "01", week: "周三", classes: "3-4", info: "语文" }, { name: "01", week: "周四", classes: "3-4", info: "体育" }, { name: "01", week: "周五", classes: "7-8", info: "英语" }, { name: "02", week: "周二", classes: "3-4", info: "语文" }, { name: "02", week: "周三", classes: "5-6", info: "数学" }, { name: "02", week: "周四", classes: "3-4", info: "政治" }, { name: "02", week: "周五", classes: "7-8", info: "英语" }, ]; const toBarList = (fooList) => { const barMap = new Map(); fooList.forEach((foo) => { const { name, week, classes, info } = foo; if (!barMap.has(name)) barMap.set(name, { name, info: [] }); const bar = barMap.get(name); bar.info.push({ week, classes, class_info: info }); }); return [...barMap.values()]; }; toBarList(fooList); |
3
hevi 2022-06-06 17:47:02 +08:00
```
const fooList = [ { name: "01", week: "周二", classes: "1-2", info: "数学" }, { name: "01", week: "周三", classes: "3-4", info: "语文" }, { name: "01", week: "周四", classes: "3-4", info: "体育" }, { name: "01", week: "周五", classes: "7-8", info: "英语" }, { name: "02", week: "周二", classes: "3-4", info: "语文" }, { name: "02", week: "周三", classes: "5-6", info: "数学" }, { name: "02", week: "周四", classes: "3-4", info: "政治" }, { name: "02", week: "周五", classes: "7-8", info: "英语" }, ]; const data = fooList.map(item=>{ const {name, week, classes, info: class_info} = item; return { name, info: { week, classes, class_info } }; }); ``` |
5
sgiyy 2022-06-06 18:09:56 +08:00
// [].reduce
// 前提数据源同 name 的是放在一起的 var old = [ { name: "01", week: "周二", classes: "1-2", info: "数学" }, { name: "01", week: "周三", classes: "3-4", info: "语文" }, { name: "01", week: "周四", classes: "3-4", info: "体育" }, { name: "01", week: "周五", classes: "7-8", info: "英语" }, { name: "02", week: "周二", classes: "3-4", info: "语文" }, { name: "02", week: "周三", classes: "5-6", info: "数学" }, { name: "02", week: "周四", classes: "3-4", info: "政治" }, { name: "02", week: "周五", classes: "7-8", info: "英语" }, ]; const transform = (list) => { return list.reduce( (acc, cur) => { let { result, current } = acc; const { name, ...rest } = cur; if (name !== current) { current = name; result = result.concat({ name, info: [rest] }); } else { result[result.length - 1].info.push(rest); } return { result, current }; }, { result: [], current: "" } ).result; }; const result = transform(old); |
6
oneisall8955 2022-06-06 18:11:24 +08:00
很基础的数据结构转换吧。科班生懂 map ,list ,遍历就行了
|
7
juventusryp OP 感谢各位回复,按照 2L 方法解决
|