Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取对象的数组';s前两个键作为新对象中的键,第三个键作为该对象中的数组_Javascript_Node.js_Json_Reactjs - Fatal编程技术网

Javascript 获取对象的数组';s前两个键作为新对象中的键,第三个键作为该对象中的数组

Javascript 获取对象的数组';s前两个键作为新对象中的键,第三个键作为该对象中的数组,javascript,node.js,json,reactjs,Javascript,Node.js,Json,Reactjs,我从csv文件中读取的数据是JSON。我希望能够从所有对象中获得测试结果,对于匹配测试,在每个对象中生成结果,并在其中包含指示符数组 [{ "Test": "GGT", "Result": "High", "Indicator": "Alcohol abuse Liver Disease" }, { "Test": "

我从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"],
    },
]

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

您想使用reducer函数,这样就可以迭代数组并返回一个新对象数组。在reducer中,您需要检查累积数组中现有的“Result”属性,然后将当前的“Indicator”添加到该对象列表中

更多信息请参见此处

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

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个小时的头。你是救命恩人。请投票表决这个问题。