Javascript : 如何将现有的 JSON 对象转换为基于自定义键的对象?
Javascript : How convert existing JSON Object into custom key based Object?
我需要 convert/destructure 我现有的 JSON 对象到一个新的基于键的表单 JSON,
请看下面的输入JSON
注意:下面的数组有 n 个对象。
输入JSON:
[
{
"processId": 95,
"pid": "PID_0",
"actionData": [
{
"actionId": 96,
"action": "Sbend",
"avg": 0.09134998917579651,
"avgVid": "",
"max": 0.13287270069122314,
"maxVid": ",
"maxPos": 1,
"min": 0.06643635034561157,
"minVid": "",
"minPos": 0,
"total": 8,
"totalTime": null
},
{
"actionId": 98,
"action": "Stand",
"avg": 0.7355453372001648,
"avgVid": "",
"max": 2.889981508255005,
"maxVid": "",
"maxPos": 11,
"min": 0.06643635034561157,
"minVid": "",
"minPos": 9,
"total": 28,
"totalTime": null
}
]
}
]
这里我需要获取 'pid' 键值并将其设置为现有 JSON 的根键,
下面有一个预期的输出
预期输出
[
"PID_0" : {
"processId": 95,
"actionData": [
{
"actionId": 96,
"action": "Sbend",
"avg": 0.09134998917579651,
"avgVid": "",
"max": 0.13287270069122314,
"maxVid": ",
"maxPos": 1,
"min": 0.06643635034561157,
"minVid": "",
"minPos": 0,
"total": 8,
"totalTime": null
},
{
"actionId": 98,
"action": "Stand",
"avg": 0.7355453372001648,
"avgVid": "",
"max": 2.889981508255005,
"maxVid": "",
"maxPos": 11,
"min": 0.06643635034561157,
"minVid": "",
"minPos": 9,
"total": 28,
"totalTime": null
}
]
}
]
我用来实现此目的的代码,但它没有给我正确的结果,
let b = Object.keys(itemsValues).reduce(
(p, c) => { console.log(p)
for (let item of itemsValues[c].actionData) {
p.pid.actionData.push(item);
}
return p;
}, {
pid: {
actionData: [],
},
}
);
请帮我解决这个问题,它真的帮了我很多。
谢谢。
- 将数组缩减为您要查找的对象作为输出
- 根据接收到的对象设置新密钥
pid
- 然后将
pid
从原来的位置移除
代码方面:
const result = input.reduce((finalObject, obj) => {
finalObject[obj.pid] = obj;
delete obj.pid;
return finalObject;
}, {});
以下是工作片段:
const input = [{
processId: 95,
pid: 'PID_0',
actionData: [{
actionId: 96,
action: 'Sbend',
avg: 0.09134998917579651,
avgVid: '',
max: 0.13287270069122314,
maxVid: '',
maxPos: 1,
min: 0.06643635034561157,
minVid: '',
minPos: 0,
total: 8,
totalTime: null,
},
{
actionId: 98,
action: 'Stand',
avg: 0.7355453372001648,
avgVid: '',
max: 2.889981508255005,
maxVid: '',
maxPos: 11,
min: 0.06643635034561157,
minVid: '',
minPos: 9,
total: 28,
totalTime: null,
},
],
}, ];
const result = input.reduce((finalObject, obj) => {
finalObject[obj.pid] = obj;
delete obj.pid;
return finalObject;
}, {});
console.log(result)
我需要 convert/destructure 我现有的 JSON 对象到一个新的基于键的表单 JSON, 请看下面的输入JSON
注意:下面的数组有 n 个对象。
输入JSON:
[
{
"processId": 95,
"pid": "PID_0",
"actionData": [
{
"actionId": 96,
"action": "Sbend",
"avg": 0.09134998917579651,
"avgVid": "",
"max": 0.13287270069122314,
"maxVid": ",
"maxPos": 1,
"min": 0.06643635034561157,
"minVid": "",
"minPos": 0,
"total": 8,
"totalTime": null
},
{
"actionId": 98,
"action": "Stand",
"avg": 0.7355453372001648,
"avgVid": "",
"max": 2.889981508255005,
"maxVid": "",
"maxPos": 11,
"min": 0.06643635034561157,
"minVid": "",
"minPos": 9,
"total": 28,
"totalTime": null
}
]
}
]
这里我需要获取 'pid' 键值并将其设置为现有 JSON 的根键, 下面有一个预期的输出
预期输出
[
"PID_0" : {
"processId": 95,
"actionData": [
{
"actionId": 96,
"action": "Sbend",
"avg": 0.09134998917579651,
"avgVid": "",
"max": 0.13287270069122314,
"maxVid": ",
"maxPos": 1,
"min": 0.06643635034561157,
"minVid": "",
"minPos": 0,
"total": 8,
"totalTime": null
},
{
"actionId": 98,
"action": "Stand",
"avg": 0.7355453372001648,
"avgVid": "",
"max": 2.889981508255005,
"maxVid": "",
"maxPos": 11,
"min": 0.06643635034561157,
"minVid": "",
"minPos": 9,
"total": 28,
"totalTime": null
}
]
}
]
我用来实现此目的的代码,但它没有给我正确的结果,
let b = Object.keys(itemsValues).reduce(
(p, c) => { console.log(p)
for (let item of itemsValues[c].actionData) {
p.pid.actionData.push(item);
}
return p;
}, {
pid: {
actionData: [],
},
}
);
请帮我解决这个问题,它真的帮了我很多。 谢谢。
- 将数组缩减为您要查找的对象作为输出
- 根据接收到的对象设置新密钥
pid
- 然后将
pid
从原来的位置移除
代码方面:
const result = input.reduce((finalObject, obj) => {
finalObject[obj.pid] = obj;
delete obj.pid;
return finalObject;
}, {});
以下是工作片段:
const input = [{
processId: 95,
pid: 'PID_0',
actionData: [{
actionId: 96,
action: 'Sbend',
avg: 0.09134998917579651,
avgVid: '',
max: 0.13287270069122314,
maxVid: '',
maxPos: 1,
min: 0.06643635034561157,
minVid: '',
minPos: 0,
total: 8,
totalTime: null,
},
{
actionId: 98,
action: 'Stand',
avg: 0.7355453372001648,
avgVid: '',
max: 2.889981508255005,
maxVid: '',
maxPos: 11,
min: 0.06643635034561157,
minVid: '',
minPos: 9,
total: 28,
totalTime: null,
},
],
}, ];
const result = input.reduce((finalObject, obj) => {
finalObject[obj.pid] = obj;
delete obj.pid;
return finalObject;
}, {});
console.log(result)