I am trying to calculate the total qualifications based on a specific attribute value (idInteraction). How can I efficiently iterate through the array?
My current approach is to iterate based on abilityOrder, but I'm having trouble matching it with idInteraction.
var arrayQ = [
'{"idInteraction":"{9ae8653e-99ef-11e9-9e08-90c283d38b9a}","abilityOrder":1, "qualification":40}',
'{"idInteraction":"{9ae8653e-99ef-11e9-9e08-90c283d38b9a}","abilityOrder":2, "qualification":60}',
'{"idInteraction":"{8ae8653e-99ef-11e9-9e08-90c283d38b9a}","abilityOrder":1, "qualification":20}',
'{"idInteraction":"{8ae8653e-99ef-11e9-9e08-90c283d38b9a}","abilityOrder":2, "qualification":30}'
];
var q = 0;
function findMinMax(arr) {
let min = JSON.parse(arr[0]).abilityOrder,
max = JSON.parse(arr[0]).abilityOrder;
for (let i = 1, len = arr.length; i < len; i++) {
let v = JSON.parse(arr[i]).abilityOrder;
min = (v < min) ? v : min;
max = (v > max) ? v : max;
}
return [min, max];
}
var maxAbility = findMinMax(arrayQ);
arrayQ.forEach(function(result, index) {
result = JSON.parse(result);
if (result.abilityOrder >= maxAbility[0] && result.idInteraction) {
q += result.qualification;
console.log('id: ' + result.idInteraction + ', q: ' + q);
q = 0;
}
});
The expected output should be:
"idInteraction":"{9ae8653e-99ef-11e9-9e08-90c283d38b9a}" - q = 100
"idInteraction":"{8ae8653e-99ef-11e9-9e08-90c283d38b9a}" - q = 50
Does anyone have any suggestions or improvements?
Thank you in advance.