javascript - 在数组 Node.js 中组合 JSON 数组

标签 javascript arrays json node.js

我正在尝试将具有 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/

相关文章:

Javascript for 循环新条件

javascript - 如何检查列表项是否已到达末尾

java - 从两个排序数组的交替元素生成所有可能的排序数组

arrays - 如何将文件读入整数数组

android - 如何使用 JSON Web 服务填充 Android 微调器?

json - 如何在写入文件之前删除文件的内容?

javascript - 使用纯 JavaScript 将 JSON 文件转换为可排序表

javascript - 使用 nyc 运行 mocha 时指定 mocha.opts 的位置?

javascript - 按多个属性对数组中的对象进行排序

java - java 中的堆空间要求 - Arrays.sort() 与 Collections.sort()