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

源数据:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值