V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
juventusryp
V2EX  ›  问与答

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

  •  
  •   juventusryp · 71 天前 · 529 次点击
    这是一个创建于 71 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题:

    • 后端返回的数据格式如下:
    [
    
    	{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 转换?

    7 条回复    2022-06-06 22:05:13 +08:00
    zhixiao
        1
    zhixiao  
       71 天前
    用 lodash 的 groupBy 转一下就行了
    rabbbit
        2
    rabbbit  
       71 天前
    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
        3
    hevi  
       71 天前
    ```
    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
        4
    hevi  
       71 天前
    @hevi 噢,没审题,别看了
    sgiyy
        5
    sgiyy  
       70 天前
    // [].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
        6
    oneisall8955  
       70 天前
    很基础的数据结构转换吧。科班生懂 map ,list ,遍历就行了
    juventusryp
        7
    juventusryp  
    OP
       70 天前
    感谢各位回复,按照 2L 方法解决
    关于   ·   帮助文档   ·   API   ·   FAQ   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   4060 人在线   最高记录 5497   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 09:49 · PVG 17:49 · LAX 02:49 · JFK 05:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.