I am trying to merge values from json array that has 2 or more arrays to one json array.
I see many similar questions but I could not find a solution. I tried a long way that did not succeed and there are certainly shorter and more beautiful ways.
I have json object like:
[
[
{
"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"
}
}
]
]
I want the result will be accoding to the subject key, if the subject key value does not exist in the other array the value should be zero.
Expected Result:
[
[
{
"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"
}
}
]
]
NOTE: The main array can be inclode more than 2 arrays.
const arr1 = [1,2,3]; const arr2 = [3,4,5]; const arr3 = [...arr1, ...arr2];