吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1007|回复: 6
收起左侧

[求助] JavaScript数据处理(对象数组操作生成新数组)

[复制链接]
a1chemy 发表于 2023-1-28 11:46
30吾爱币
请问各位大佬,后端给的数据是这样的:
[JavaScript] 纯文本查看 复制代码
[
    {
        "Id": 11,
        "PhoneUnit": "后勤部",
        "PhoneName": "张三",
        "Phone": "19100000000"
    },
	 {
        "Id": 12,
        "PhoneUnit": "后勤部",
        "PhoneName": "李四",
        "Phone": "19100000001"
    },
	 {
        "Id": 13,
        "PhoneUnit": "开发部",
        "PhoneName": "王五",
        "Phone": "19100000002"
    },
	 {
        "Id": 14,
        "PhoneUnit": "开发部",
        "PhoneName": "孙权",
        "Phone": "19100000003"
    },
	 {
        "Id": 15,
        "PhoneUnit": "办公室",
        "PhoneName": "张飞",
        "Phone": "19100000004"
    }
    
]

我想要这种格式的:
[JavaScript] 纯文本查看 复制代码
{
	"后勤部": {
		"employees": [
			{
				"Id": 11,
				"PhoneUnit": "后勤部",
				"PhoneName": "张三",
				"Phone": "19100000000"
			},
			{
				"Id": 12,
				"PhoneUnit": "后勤部",
				"PhoneName": "李四",
				"Phone": "19100000001"
			}
		]
	},
	"开发部": {
		"employees": [
			{
				"Id": 13,
				"PhoneUnit": "开发部",
				"PhoneName": "王五",
				"Phone": "19100000002"
			},
			{
				"Id": 14,
				"PhoneUnit": "开发部",
				"PhoneName": "孙权",
				"Phone": "19100000003"
			}
		]
	},
	"办公室": {
		"employees": [
			{
				"Id": 15,
				"PhoneUnit": "办公室",
				"PhoneName": "张飞",
				"Phone": "19100000004"
			}
		]
	}
}

新手一个,还请大佬教一下,非常感谢!

最佳答案

查看完整内容

建议让后端处理。 [mw_shl_code=html,true] Document var oldList = [ { Id: 11, PhoneUnit: '后勤部', PhoneName: '张三', Phone: '19100000000', }, { Id: 12, PhoneUnit: '后勤部', PhoneName: '李四', Phone: '19100000001', }, ...

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

waf642311 发表于 2023-1-28 11:46
建议让后端处理。
[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var oldList = [
        {
          Id: 11,
          PhoneUnit: '后勤部',
          PhoneName: '张三',
          Phone: '19100000000',
        },
        {
          Id: 12,
          PhoneUnit: '后勤部',
          PhoneName: '李四',
          Phone: '19100000001',
        },
        {
          Id: 13,
          PhoneUnit: '开发部',
          PhoneName: '王五',
          Phone: '19100000002',
        },
        {
          Id: 14,
          PhoneUnit: '开发部',
          PhoneName: '孙权',
          Phone: '19100000003',
        },
        {
          Id: 15,
          PhoneUnit: '办公室',
          PhoneName: '张飞',
          Phone: '19100000004',
        },
      ];

      var keys = new Set(oldList.map((v) => v.PhoneUnit));
      var newList = [];
      keys.forEach((v) => {
        newList[v] = { employees: [] };
      });
      oldList.forEach((v) => {
        newList[v.PhoneUnit].employees.push(v);
      });
      console.log(newList);
    </script>
  </body>
</html>
feiyu361 发表于 2023-1-28 13:40
头像被屏蔽
saszznz 发表于 2023-1-28 13:47
npc404 发表于 2023-1-28 13:52
[JavaScript] 纯文本查看 复制代码
let arr = [
        {
          Id: 11,
          PhoneUnit: '后勤部',
          PhoneName: '张三',
          Phone: '19100000000',
        },
        {
          Id: 12,
          PhoneUnit: '后勤部',
          PhoneName: '李四',
          Phone: '19100000001',
        },
        {
          Id: 13,
          PhoneUnit: '开发部',
          PhoneName: '王五',
          Phone: '19100000002',
        },
        {
          Id: 14,
          PhoneUnit: '开发部',
          PhoneName: '孙权',
          Phone: '19100000003',
        },
        {
          Id: 15,
          PhoneUnit: '办公室',
          PhoneName: '张飞',
          Phone: '19100000004',
        },
      ]
      const result = {}
      arr.forEach(item => {
        if (item.PhoneUnit in result) {
          result[item.PhoneUnit].employees.push(item)
        } else {
          result[item.PhoneUnit] = { employees: [item] }
        }
      })

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
a1chemy + 1 + 1 谢谢@Thanks!

查看全部评分

Justin0110 发表于 2023-1-29 09:14
npc404 发表于 2023-1-28 13:52
[mw_shl_code=javascript,true]let arr = [
        {
          Id: 11,

这个 就很棒~
iLouis 发表于 2023-1-30 13:28
碰到这种情况我一般会让后端以我要的方式给数据给我。
除了三种情况:
一、这份数据我多处使用,而且是不同格式,反正都是要重新格式化的。
二、后端不在,情况紧急。
三、还在开发阶段,后端还没搞定之前,先自己格式化,继续写后面的,等到后端改好,我这边把格式化去掉。

我一般会这么处理,因为大部分情况下,key还是有用的,我会先把key先提出来。
[JavaScript] 纯文本查看 复制代码
const resData = [{
    "Id": 11,
    "PhoneUnit": "后勤部",
    "PhoneName": "张三",
    "Phone": "19100000000"
}, {
    "Id": 12,
    "PhoneUnit": "后勤部",
    "PhoneName": "李四",
    "Phone": "19100000001"
}, {
    "Id": 13,
    "PhoneUnit": "开发部",
    "PhoneName": "王五",
    "Phone": "19100000002"
}, {
    "Id": 14,
    "PhoneUnit": "开发部",
    "PhoneName": "孙权",
    "Phone": "19100000003"
}, {
    "Id": 15,
    "PhoneUnit": "办公室",
    "PhoneName": "张飞",
    "Phone": "19100000004"
}
]
const keyList = Array.from(new Set(res.map(el => el.PhoneUnit)))
let result = keyList.map(key => ({ [key]: { employees: res.filter(el => el.PhoneUnit === key) } }))
result = result.length ? result.reduce((a, b) => ({ ...a, ...b })) : result
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-1-11 17:58

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表