在阵列Node.js中组合JSON数组

时间:2018-01-30 11:59:36

标签: 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个以上的阵列。

3 个答案:

答案 0 :(得分:3)

我想a是你给定的JSON数组。

facebook-square

答案 1 :(得分:1)

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



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);
&#13;
.hidden {
  display: none;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我对工作进行了划分,以便在主阵列中只有两个阵列时更容易找到解决方案。

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

希望这会有所帮助。

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>