我正在尝试将具有 2 个或更多数组的 json 数组的值合并到一个 json 数组。
我看到很多类似的问题,但找不到解决方案。我尝试了很长的路,但没有成功,当然还有更短、更漂亮的路。
我有像这样的 json 对象:
[
[
{
"f_measure_function": {
"value": {
"item_type": "ST",
"Randomization number": "R02-03"
},
"subject": "001-BB"
}
},
{
"f_measure_function": {
"value": {
"item_type": "ST",
"Randomization number": "R02-03"
},
"subject": "002-CC"
}
},
{
"f_measure_function": {
"value": {
"item_type": "ST",
"Randomization number": "R01-02"
},
"subject": "010-A1"
}
},
{
"f_measure_function": {
"value": {
"item_type": "ST",
"Randomization number": "R33-33"
},
"subject": "011-AB"
}
}
],
[
{
"f_measure_function": {
"value": {
"item_type": "INT",
"Serial number of device given to the subject": "13032355"
},
"subject": "001-BB"
}
},
{
"f_measure_function": {
"value": {
"item_type": "INT",
"Serial number of device given to the subject": "13032355"
},
"subject": "002-CC"
}
},
{
"f_measure_function": {
"value": {
"item_type": "INT",
"Serial number of device given to the subject": "13032333"
},
"subject": "99-001"
}
},
{
"f_measure_function": {
"value": {
"item_type": "INT",
"Serial number of device given to the subject": "13032111"
},
"subject": "test-111"
}
}
]
]
我希望结果将根据主题键进行编码,如果其他数组中不存在主题键值,则该值应为零。
预期结果:
[
[
{
"f_measure_function": {
"value0": {
"item_type": "ST",
"Randomization number": "R02-03"
},
"value1": {
"item_type": "INT",
"Serial number of device given to the subject": "13032355"
},
"subject": "001-BB"
}
},
{
"f_measure_function": {
"value0": {
"item_type": "ST",
"Randomization number": "R02-03"
},
"value1": {
"item_type": "INT",
"Serial number of device given to the subject": "13032355"
},
"subject": "002-CC"
}
},
{
"f_measure_function": {
"value0": {
"item_type": "ST",
"Randomization number": "R01-02"
},
"value1": {
"item_type": "INT",
"Serial number of device given to the subject": null
},
"subject": "010-A1"
}
},
{
"f_measure_function": {
"value0": {
"item_type": "ST",
"Randomization number": "R33-33"
},
"value1": {
"item_type": "INT",
"Serial number of device given to the subject": null
},
"subject": "011-AB"
}
},
{
"f_measure_function": {
"value0": {
"item_type": "ST",
"Randomization number": null
},
"value1": {
"item_type": "INT",
"Serial number of device given to the subject": "13032333"
},
"subject": "99-001"
}
},
{
"f_measure_function": {
"value0": {
"item_type": "ST",
"Randomization number": null
},
"value1": {
"item_type": "INT",
"Serial number of device given to the subject": "13032111"
},
"subject": "test-111"
}
}
]
]
注意:主数组可以包含 2 个以上的数组。
最佳答案
我想 a 是您给定的 JSON 数组。
let finalArray = []
/* putting all single json in finalArray */
let b = a.map(value => {
value.map(val => {
finalArray.push(val)
})
})
let v = []
finalArray.map((val, idx) => {
/* filtering all duplicate subjects and putting in to another array v */
v[idx] = finalArray.filter(value => {
return val.f_measure_function.subject == value.f_measure_function.subject
})
})
let c = v.map(value => {
/* creating the final array as you want */
let va = {"f_measure_function": {}}
value.forEach((val, idx) => {
va.f_measure_function["value" + idx] = val.f_measure_function.value
})
va.f_measure_function["subject"] = value[0].f_measure_function.subject
return va
})
console.log(JSON.stringify(c))
关于javascript - 在数组 Node.js 中组合 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48520967/