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

2023-12-13 18:53:46

源数据:

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"
				}]

目标数据:

[{
	"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"
	}]
}]

格式化数据方法:

格式化数据方法:
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))
			},

文章来源:https://blog.csdn.net/lmy_loveF/article/details/134971045
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。