js 下对后端返回格式的转换

2022-06-06 17:05:58 +08:00
 juventusryp

问题:

[

	{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 转换?

1016 次点击
所在节点    问与答
7 条回复
zhixiao
2022-06-06 17:11:14 +08:00
用 lodash 的 groupBy 转一下就行了
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);
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
}
};
});
```
hevi
2022-06-06 17:49:26 +08:00
@hevi 噢,没审题,别看了
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);
oneisall8955
2022-06-06 18:11:24 +08:00
很基础的数据结构转换吧。科班生懂 map ,list ,遍历就行了
juventusryp
2022-06-06 22:05:13 +08:00
感谢各位回复,按照 2L 方法解决

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/857634

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX