Here is the code that I am working with:
var input=require('./task.json');
const _ = require(`underscore`);
var dps = [];
for(var element in input) {
for (var i=0;i>dps.length;i++){
if(dps[i].Technician===input[element].Technician){
console.log("name already exists");
dps[i].count=dps[i].Count+1;
}
}
dps.push({Technician: input[element].Technician, Count:0});
}
console.log(dps);
This is how my task.json file is structured:
{{
"TaskID": 35708,
"TaskDate": "2011-06-20T00:00:00",
"Technician": "UCH - Billy Metcalf"
},
{
"TaskID": 35708,
"TaskDate": "2011-06-19T00:00:00",
"Technician": "Edward F. Dawson"
}
}
When I execute my file using node on data.js, I get a result like this:
[{Technician:'UCH - Billy Metcalf',Count:0},
{Technician:'Edward F. Dawson',Count:0}]
The main objective is to make sure that instead of always setting the count to zero, the program should go through the json file and if it encounters a duplicate entry in the new variable dps, it should increment the count. The new array dps should be unique without any repeating names. For instance, if there are 5 instances of "Edward F. Dawson", then
{Technician:'Edward F. Dawson',Count:5}