【问题标题】:get array of object's first two keys as keys in new object and third key as array in that object将对象的前两个键的数组作为新对象中的键,将第三个键作为该对象中的数组
【发布时间】:2021-06-19 22:31:52
【问题描述】:

我有从 csv 文件中读取的 JSON 数据。我希望能够从所有对象中获取测试、结果,并在每个对象中匹配测试、结果并在其中包含指标数组。

[{
    "Test": "GGT",
    "Result": "High",
    "Indicator": "Alcohol abuse Liver Disease"
}, {
    "Test": "GGT",
    "Result": "High",
    "Indicator": "Congestive Heart Failure"
}, {
    "Test": "GGT",
    "Result": "High",
    "Indicator": "Drugs: NSAIDs"
}, {
    "Test": "GGT",
    "Result": "High",
    "Indicator": "Drugs: Lipid lowering drugs"
}, {
    "Test": "GGT",
    "Result": "High",
    "Indicator": "Drugs: Antibiotics"
}, {
    "Test": "GGT",
    "Result": "High",
    "Indicator": "Drugs: Histamine Blockers"
}, {
    "Test": "GGT",
    "Result": "High",
    "Indicator": "Drugs: Antifungals"
}, {
    "Test": "GGT",
    "Result": "High",
    "Indicator": "Drugs: anticonvulsants"
}, {
    "Test": "GGT",
    "Result": "High",
    "Indicator": "Drugs: antidepressants"
}, {
    "Test": "GGT",
    "Result": "High",
    "Indicator": "Drugs: testoserone"
}, {
    "Test": "GGT",
    "Result": "Low",
    "Indicator": "No concern"
}, {
    "Test": "GGT",
    "Result": "Low",
    "Indicator": "unlikely liver disease"
}, {
    "Test": "GGT",
    "Result": "Low",
    "Indicator": "Drugs: Oral contraceptives"
}, {
    "Test": "GGT",
    "Result": "Low",
    "Indicator": "Drugs: Clofibrate"
}, {
    "Test": "AST",
    "Result": "High",
    "Indicator": "(if >10xULN) acute hepatitis"
}]

我希望能够像这样解析它

[
    {
        Test: "GGT",
        Result: "High",
        Indicators: [
            "Alcohol abuse Liver Disease",
            "Congestive Heart Failure",
            "Drugs: NSAIDs",
            "Drugs: Lipid lowering drugs",
            "Drugs: Antibiotics",
            "Drugs: Histamine Blockers",
        ],
    },
    {
        Test: "GGT",
        Result: "Low",
        Indicators: ["unlikely liver disease", "Drugs: Oral contraceptives", "Drugs: Clofibrate"],
    },
]

我跳过了结果中的记录,但我相信你明白了。我想将测试、结果键映射在一起并制作一个指标数组。请帮助我。

【问题讨论】:

    标签: javascript node.js json reactjs


    【解决方案1】:

    您想使用 reducer 函数,因此您可以遍历数组并返回一个新对象数组。在 reducer 中,您可能希望检查累积数组中是否存在现有的“Result”属性,然后将当前的“Indicator”添加到该对象列表中。

    查看这里了解更多信息https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

    对我来说,它看起来像这样:

    const list = [{a: 'v1', b: 'value'},{a: 'v2', b: 'value'},{a: 'v1', b: 'value2'}];
    
    const result = list.reduce((accumulator, current) => {
      const idx = accumulator.findIndex((item) => item.a == current.a);
      if(idx == -1) {
        accumulator.push({a: current.a, values: [current.b]});
        return accumulator;
      }
      accumulator[idx].values.push(current.b);
      return accumulator;
    }, []);
    

    【讨论】:

    • 非常感谢。我已经挠头 12 个小时了。你是救生员。请投票赞成这个问题。
    猜你喜欢
    • 1970-01-01
    • 2016-09-06
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 2022-09-22
    • 2020-02-07
    • 1970-01-01
    • 2021-06-15
    相关资源
    最近更新 更多