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

js JSON数据根据不同类别重组_第1张图片

源数据:

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))
			},

你可能感兴趣的:(javascript,前端,java)