js JSON数据根据不同类别重组

源数据:

javascript 复制代码
this.deviceTypeMap= 
{
	"113": "G3P国内定位器",
	"114": "铁将军-火星人2300",
	"139": "龙嘉4G-带CAN"
}
this.tmpData=
				[{
					"ibdr_pdid": "113",
					"ibdr_default_device": "10",
					"ibdr_devsn": "1135257499235",
					"ibdr_last_time": "2023-12-13 11:13:34",
					"ibdr_name": "1135257499235"
				}, {
					"ibdr_pdid": "114",
					"ibdr_default_device": "20",
					"ibdr_devsn": "1141553148067",
					"ibdr_last_time": "2023-12-13 11:13:31",
					"ibdr_name": "1141553148067"
				}, {
					"ibdr_pdid": "139",
					"ibdr_default_device": "20",
					"ibdr_devsn": "1394343434349",
					"ibdr_last_time": "2023-12-13 11:13:27",
					"ibdr_name": "1394343434349"
				}, {
					"ibdr_pdid": "113",
					"ibdr_default_device": "20",
					"ibdr_devsn": "1135257511548",
					"ibdr_last_time": "2023-12-13 11:13:34",
					"ibdr_name": "1135257511548"
				}, {
					"ibdr_pdid": "114",
					"ibdr_default_device": "20",
					"ibdr_devsn": "1141540433527",
					"ibdr_last_time": "2023-12-13 11:13:31",
					"ibdr_name": "1141540433527"
				}, {
					"ibdr_pdid": "114",
					"ibdr_default_device": "20",
					"ibdr_devsn": "1141528873710",
					"ibdr_last_time": "2023-12-13 11:13:30",
					"ibdr_name": "1141528873710"
				}]

目标数据:

javascript 复制代码
[{
	"title": "113-G3P国内定位器",
	"equipmentList": [{
		"ibdr_pdid": "113",
		"ibdr_default_device": "10",
		"ibdr_devsn": "1135257499235",
		"ibdr_last_time": "2023-12-13 11:13:34",
		"ibdr_name": "1135257499235"
	}, {
		"ibdr_pdid": "113",
		"ibdr_default_device": "20",
		"ibdr_devsn": "1135257511548",
		"ibdr_last_time": "2023-12-13 11:13:34",
		"ibdr_name": "1135257511548"
	}]
}, {
	"title": "114-铁将军-火星人2300",
	"equipmentList": [{
		"ibdr_pdid": "114",
		"ibdr_default_device": "20",
		"ibdr_devsn": "1141553148067",
		"ibdr_last_time": "2023-12-13 11:13:31",
		"ibdr_name": "1141553148067"
	}, {
		"ibdr_pdid": "114",
		"ibdr_default_device": "20",
		"ibdr_devsn": "1141540433527",
		"ibdr_last_time": "2023-12-13 11:13:31",
		"ibdr_name": "1141540433527"
	}, {
		"ibdr_pdid": "114",
		"ibdr_default_device": "20",
		"ibdr_devsn": "1141528873710",
		"ibdr_last_time": "2023-12-13 11:13:30",
		"ibdr_name": "1141528873710"
	}]
}, {
	"title": "139-龙嘉4G-带CAN",
	"equipmentList": [{
		"ibdr_pdid": "139",
		"ibdr_default_device": "20",
		"ibdr_devsn": "1394343434349",
		"ibdr_last_time": "2023-12-13 11:13:27",
		"ibdr_name": "1394343434349"
	}]
}]

格式化数据方法:

javascript 复制代码
格式化数据方法:
setData(data, deviceTypeMap) {
				let pdids = []
				data.forEach(dev => {
					if (!pdids.includes(dev.ibdr_pdid)) {
						pdids.push(dev.ibdr_pdid)
					}
				})
				pdids = pdids.sort() // 按pdid从小到大排序
				const tempDataList = []
				pdids.forEach(pdid => {
					let title = ''
					const name = deviceTypeMap[pdid]
					if (name) {
						title = pdid + '-' + name
					}
					const item = {
						title: title,
						equipmentList: []
					}
					data.forEach(dev => {
						if (pdid === dev.ibdr_pdid) {
							item.equipmentList.push(dev)
						}
					})
					tempDataList.push(item)
				})
				
				this.typeList = tempDataList
				console.log("this.typeList",JSON.stringify(this.typeList))
			},
相关推荐
有梦想的刺儿1 分钟前
webWorker基本用法
前端·javascript·vue.js
P.H. Infinity6 分钟前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天10 分钟前
java的threadlocal为何内存泄漏
java
caridle21 分钟前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
cy玩具22 分钟前
点击评论详情,跳到评论页面,携带对象参数写法:
前端
^velpro^26 分钟前
数据库连接池的创建
java·开发语言·数据库
苹果醋330 分钟前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
秋の花35 分钟前
【JAVA基础】Java集合基础
java·开发语言·windows
小松学前端37 分钟前
第六章 7.0 LinkList
java·开发语言·网络
Wx-bishekaifayuan44 分钟前
django电商易购系统-计算机设计毕业源码61059
java·spring boot·spring·spring cloud·django·sqlite·guava