I have a json data like in the block below. Same data are "id", "ip", "port", "name" and "mac_id" . Difference data are "cam_id","dvr_analog_channel" and "cam _name".
[
{
"id": 76,
"ip": "159.146.00.11",
"port": 5000,
"name": "Server",
"mac_id": "asdasdaad121",
"cam_id": 15363,
"dvr_analog_channel": 33,
"cam_name": "IP Channel 33"
},
{
"id": 76,
"ip": "159.146.00.11",
"port": 5000,
"name": "Server",
"mac_id": "asdasdaad121",
"cam_id": 15364,
"dvr_analog_channel": 34,
"cam_name": "IP Channel 34"
},
{
"id": 76,
"ip": "159.146.00.11",
"port": 5000,
"name": "Server",
"mac_id": "asdasdaad121",
"cam_id": 15365,
"dvr_analog_channel": 35,
"cam_name": "IP Channel 35"
}
]
I want to above json data like this to JavaScript.
[
{
"id": 76,
"serverIp": "159.146.00.11",
"serverPort": 5000,
"serverName": "Server",
"serverMacId": "asdasdaad121",
"channels": [
{
"id": 15362,
"channelNo": 33,
"name": "IP Channel 33"
},
{
"id": 15363,
"channelNo": 34,
"name": "IP Channel 34"
},
{
"id": 15364,
"channelNo": 35,
"name": "IP Channel 35"
}
]
}
]
How to convert this json?
CodePudding user response:
const data = [
{
"id": 76,
"ip": "159.146.00.11",
"port": 5000,
"name": "Server",
"mac_id": "asdasdaad121",
"cam_id": 15363,
"dvr_analog_channel": 36,
"cam_name": "IP Channel 34"
},
{
"id": 76,
"ip": "159.146.00.11",
"port": 5000,
"name": "Server",
"mac_id": "asdasdaad121",
"cam_id": 15364,
"dvr_analog_channel": 35,
"cam_name": "IP Channel 35"
},
{
"id": 76,
"ip": "159.146.00.11",
"port": 5000,
"name": "Server",
"mac_id": "asdasdaad121",
"cam_id": 15365,
"dvr_analog_channel": 36,
"cam_name": "IP Channel 36"
}
]
my solution here we go
const result = data.reduce((accumulator, current) => {
if (accumulator?.length === 0) {
accumulator[0] = {
id: current.id,
ip: current.ip,
port: current.port,
name: current.name,
mac_id: current.mac_id,
nestedJson: [
{
cam_id: current.cam_id,
dvr_analog_channel: current.dvr_analog_channel,
cam_name: current.cam_name,
},
],
};
} else {
const index = accumulator.findIndex(
(item) => item.mac_id === current.mac_id
);
if (index !== -1) {
accumulator[index].nestedJson.push({
cam_id: current.cam_id,
dvr_analog_channel: current.dvr_analog_channel,
cam_name: current.cam_name,
});
} else {
accumulator[accumulator.length] = {
id: current.id,
ip: current.ip,
port: current.port,
name: current.name,
mac_id: current.mac_id,
nestedJson: [
{
cam_id: current.cam_id,
dvr_analog_channel: current.dvr_analog_channel,
cam_name: current.cam_name,
},
],
};
}
}
return accumulator;
}, []);
result will be
[
{
"id": 76,
"ip": "159.146.00.11",
"port": 5000,
"name": "Server",
"mac_id": "asdasdaad121",
"nestedJson": [
{
"cam_id": 15363,
"dvr_analog_channel": 36,
"cam_name": "IP Channel 34"
},
{
"cam_id": 15364,
"dvr_analog_channel": 35,
"cam_name": "IP Channel 35"
},
{
"cam_id": 15365,
"dvr_analog_channel": 36,
"cam_name": "IP Channel 36"
}
]
}
]
CodePudding user response:
Here is another way to achieve the result:
const data = [{
id: 76,
ip: '159.146.00.11',
port: 5000,
name: 'Server',
mac_id: 'asdasdaad121',
cam_id: 15363,
dvr_analog_channel: 36,
cam_name: 'IP Channel 34',
},
{
id: 76,
ip: '159.146.00.11',
port: 5000,
name: 'Server',
mac_id: 'asdasdaad121',
cam_id: 15364,
dvr_analog_channel: 35,
cam_name: 'IP Channel 35',
},
{
id: 76,
ip: '159.146.00.11',
port: 5000,
name: 'Server',
mac_id: 'asdasdaad121',
cam_id: 15365,
dvr_analog_channel: 36,
cam_name: 'IP Channel 36',
},
]
const result = data.reduce((a, c) => {
const { cam_id, dvr_analog_channel, cam_name, ...rest } = c
const cameras = { cam_id, dvr_analog_channel, cam_name }
const exist = a.find(obj => obj.id === c.id)
if (exist) {
if (exist.channels) exist.channels.push(cameras)
else exist.channels = [cameras]
} else {
a.push({ ...rest, channels: [cameras] })
}
return a
}, [])
console.log(JSON.stringify(result, null, 2))
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>