I have done the pre-processing calculation using javascript and finally have the following different arrays.
var medianArray = [{ Thomas: 94, Diana: 94, Claura: 93, Chandler: 91 }];
var q1Array = [ { Thomas: 93, Diana: 94, Claura: 92, Chandler: 91 } ];
var q3Array = [{ Thomas: 95, Diana: 95, Claura: 93, Chandler: 93 }];
var outArray = [{ Thomas: [], Diana: [85, 86, 87, 91], Claura: [94, 94, 94], Chandler: [75, 86] }];
var result = [];
now, I am trying to bring into a single array with meaningful keys and values in the below format.
[{"stdName": "Thomas", "q1" : 93, "q3" : 95, "median": 94, "outliers": []},
{"stdName": "Diana", "q1" : 93, "q3" : 95, "median": 94, "outliers": [85, 86, 87, 91]},
{"stdName": "Claura", "q1" : 93, "q3" : 95, "median": 93, "outliers": [94, 94, 94]},
{"stdName": "Chandler", "q1" : 93, "q3" : 95, "median": 91, "outliers": [75, 86]}]
I have searched tried multiple online resources and references of reduce(), map(), etc., but somehow not getting how to change the key name solutions.
[].concat(q1Array, q3Array, medianArray,outArray).forEach(function (items) {
//logic try to add and not getting idea to proceed.
if (!this[items]) {
result.push();
}
}, Object.create(null));
console.log(result);
I hope you kind of JavaScript people on Stack Overflow can help me out with this.
If all arrays have the same amount of names you can first extract all names with Object.keys(). Afterwards loop over all the names and reduce them into their own object.
var medianArray = [{ Thomas: 94, Diana: 94, Claura: 93, Chandler: 91 }];
var q1Array = [ { Thomas: 93, Diana: 94, Claura: 92, Chandler: 91 } ];
var q3Array = [{ Thomas: 95, Diana: 95, Claura: 93, Chandler: 93 }];
var outArray = [{ Thomas: [], Diana: [85, 86, 87, 91], Claura: [94, 94, 94], Chandler: [75, 86] }];
const names = Object.keys(medianArray[0]);
const result = names.reduce((acc, name) => {
acc.push({
stdName: name,
q1: q1Array[0][name],
q3: q3Array[0][name],
median: medianArray[0][name],
outliers: outArray[0][name],
});
return acc;
}, []);
console.log(result);