【问题标题】:Combine JSON array inside array Node.js在数组 Node.js 中合并 JSON 数组
【发布时间】:2018-01-30 11:59:36
【问题描述】:

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

注意:主数组可以包含两个以上的数组。

【问题讨论】:

  • 那么你尝试了什么?做到这一点并不难.. 一路递归 ;)
  • 你可以这样做const arr1 = [1,2,3]; const arr2 = [3,4,5]; const arr3 = [...arr1, ...arr2];
  • @GuyT - 谢谢,你能给我举个例子吗?
  • @Majid 感谢您的回复。我需要将它们组合起来以连接它们。

标签: javascript arrays json node.js


【解决方案1】:

我想 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))

【讨论】:

  • 您的回复!,看起来很不错。但它返回 json 如下:{ "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" } }
  • 我认为它会按照您的描述生成输出。请告诉我哪里出错了!
  • 我的错,对不起。我从错误的地方返回了 json 对象。非常感谢你!
  • 在结果中我有重复的行,每个主题有 2 行 :(
  • 在你的代码末尾我添加了:var uniq = new Set(c.map(e => JSON.stringify(e))); var res = Array.from(uniq).map(e => JSON.parse(e)); 并修复了它:)
【解决方案2】:

我正在使用一个对象来帮助解析。可能有更好的解决方案,但这个可以解决问题!

var JSONstring = document.querySelector('#JSONstring').textContent;

var parsedJSON = JSON.parse(JSONstring);

var results = [];

var resultsObj = {};

var maxValLen = 0;

for (var i=0; i<parsedJSON.length; i++) {
  var array = parsedJSON[i];
  for (var j=0; j<array.length; j++) {
    var item = array[j].f_measure_function;
    if (!resultsObj[item.subject]) {
      resultsObj[item.subject] = [];
    }
    resultsObj[item.subject].push(item.value);
    if (resultsObj[item.subject].length > maxValLen) {
      maxValLen = resultsObj[item.subject].length;
    }
  }
}

for (var subject in resultsObj) {
  var valuesArray = resultsObj[subject];
  var measure = {
    subject: subject
  };
  for (var v=0; v<maxValLen; v++) {
    measure['value'+v] = valuesArray[v] || "null";
  }
  results.push({
    f_measure_function: measure
  })
}

console.log(results);
.hidden {
  display: none;
}
<code id="JSONstring" class="hidden">
[
    [
        {
            "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"
            }
        }
    ]
]
</code>

【讨论】:

  • 谢谢!但是在您的结果中,每个主题只有 value0,每个主题应该有 2 个值,value0 和 value1(在这种情况下),每个数组应该是 valueX、value0、value1 等等。请看我的预期结果
  • 抱歉,打错字了……已修复。
  • 非常感谢!它工作正常!我更喜欢另一种更简短的解决方案。
  • 如果特定主题没有值,则应为空\零,例如:“010-A1”主题的value1应显示为空值
  • 已更新,一切正常!我以为你会用另一种解决方案。发生了什么?
【解决方案3】:

我对工作进行了划分,以便在主数组中只有两个数组的情况下更容易得到解决方案。

关于注意表明主数组可能包含两个以上的数组我认为您应该指出合并将如何完成,因为它看起来不明确

希望这会有所帮助。

let arr = [
    [
        {
            "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"
            }
        }
    ]
]

//first array containing the subjects of the first array
let subArr1 = arr[0].map((e) => e.f_measure_function.subject);
//second array containing the subjects of the second array
let subArr2 = arr[1].map((e) => e.f_measure_function.subject);

//our final array that will contain the result
let finalArray = [];

for (let i = 0; i < subArr1.length; i++) {
    
    //the sample object for every new item and we will only modify three values of it
    const newObject = {
        "f_measure_function": {
            "value0": {
                "item_type": "ST",
                "Randomization number": ""
            },
            "value1": {
                "item_type": "INT",
                "Serial number of device given to the subject": ""
            },
            "subject": ""
        }
    };
    let subj = subArr1[i];
    newObject.f_measure_function.value0["Randomization number"] =
    arr[0][i].f_measure_function.value["Randomization number"];
     
    newObject.f_measure_function.subject = subj;  
    
    //test if the subject exists in the second array
    if (subArr2.includes(subj)) {
        newObject.f_measure_function.value1 = arr[1][i]["f_measure_function"]["value"];
    }
    else {
        newObject.f_measure_function.value1["Serial number of device given to the subject"]= null;
    }

    finalArray.push(newObject);
}

for (let i = 0; i < subArr2.length; i++) {
    const newObject = {
        "f_measure_function": {
            "value0": {
                "item_type": "ST",
                "Randomization number": ""
            },
            "value1": {
                "item_type": "INT",
                "Serial number of device given to the subject": ""
            },
            "subject": ""
        }
    };
    let subj =subArr2[i];
    newObject.f_measure_function.value1 = arr[1][i].f_measure_function.value;
    newObject.f_measure_function.subject = subj;
    
    
    //test if the subject exists in the first array
    if (subArr1.includes(subj)) {
        newObject.f_measure_function.value0["Randomization number"] = arr[0][i].f_measure_function.value["Randomization number"];
    }
    else {
        newObject.f_measure_function.value0["Randomization number"] = null;
    }
    //console.log(newObject)
    finalArray.push(newObject); 
}
//console.log(finalArray);

const pre = document.getElementById("result");
pre.innerHTML =JSON.stringify(finalArray,undefined,2);
<pre id="result" class="prettyprint">
    
</pre>

【讨论】:

  • 感谢您的回复,我更喜欢另一种更简短的解决方案。
  • 但我认为其他答案没有给出正确的输出,因为没有 value0 或 value1 的结果。
猜你喜欢
  • 2023-03-16
  • 2017-02-24
  • 1970-01-01
  • 2020-08-15
  • 2019-08-30
  • 2014-01-24
  • 1970-01-01
  • 2021-11-24
相关资源
最近更新 更多