Reputation: 11
I need to make a filter in an array, also need to have a counter to know how many records it has as the same id. I tried to do with the filter but I did not get it because I did not know what to compare it with. it is a list of students, it has only 3 different ones, and it repeats itself several times, because it is a list of medicines that are repeated for students.
var arr = [
{
"gl90Tabl": null,
"linkRece": "",
"co01Apme": 5359,
"co90Alun": 921,
"co00Apme": 281,
"dataApme": "2019-03-06",
"horaApme": "10:00",
"nomeApme": "Tylenol",
"dosaApme": "10 gotas",
"viadApme": "Oral",
"obsApme": "Aplicar mesmo que chore.",
"situApme": "PEND",
"dhapApme": null
},
{
"gl90Tabl": null,
"linkRece": "",
"co01Apme": 5360,
"co90Alun": 921,
"co00Apme": 281,
"dataApme": "2019-03-06",
"horaApme": "11:00",
"nomeApme": "Tylenol",
"dosaApme": "10 gotas",
"viadApme": "Oral",
"obsApme": "Aplicar mesmo que chore.",
"situApme": "PEND",
"dhapApme": null
},
{
"gl90Tabl": null,
"linkRece": "",
"co01Apme": 5361,
"co90Alun": 921,
"co00Apme": 281,
"dataApme": "2019-03-06",
"horaApme": "12:00",
"nomeApme": "Tylenol",
"dosaApme": "10 gotas",
"viadApme": "Oral",
"obsApme": "Aplicar mesmo que chore.",
"situApme": "PEND",
"dhapApme": null
},
{
"gl90Tabl": null,
"linkRece": "",
"co01Apme": 5362,
"co90Alun": 921,
"co00Apme": 281,
"dataApme": "2019-03-06",
"horaApme": "14:00",
"nomeApme": "Tylenol",
"dosaApme": "10 gotas",
"viadApme": "Oral",
"obsApme": "Aplicar mesmo que chore.",
"situApme": "PEND",
"dhapApme": null
},
{
"gl90Tabl": null,
"linkRece": "",
"co01Apme": 5363,
"co90Alun": 921,
"co00Apme": 281,
"dataApme": "2019-03-06",
"horaApme": "16:00",
"nomeApme": "Tylenol",
"dosaApme": "10 gotas",
"viadApme": "Oral",
"obsApme": "Aplicar mesmo que chore.",
"situApme": "PEND",
"dhapApme": null
},
{
"gl90Tabl": null,
"linkRece": "",
"co01Apme": 5340,
"co90Alun": 2601,
"co00Apme": 279,
"dataApme": "2019-03-06",
"horaApme": "16:00",
"nomeApme": "Aspirina",
"dosaApme": "10 Gotas",
"viadApme": "Oral",
"obsApme": "Aplicar mesmo que chore",
"situApme": "PEND",
"dhapApme": null
},
{
"gl90Tabl": null,
"linkRece": "",
"co01Apme": 5357,
"co90Alun": 455,
"co00Apme": 280,
"dataApme": "2019-03-06",
"horaApme": "22:00",
"nomeApme": "Dipirona",
"dosaApme": "10ml",
"viadApme": "Oral",
"obsApme": "",
"situApme": "PEND",
"dhapApme": null
},
{
"gl90Tabl": null,
"linkRece": "",
"co01Apme": 5358,
"co90Alun": 455,
"co00Apme": 280,
"dataApme": "2019-03-06",
"horaApme": "23:00",
"nomeApme": "Dipirona",
"dosaApme": "10ml",
"viadApme": "Oral",
"obsApme": "",
"situApme": "PEND",
"dhapApme": null
}
]
console.log(arr)
Upvotes: 1
Views: 295
Reputation: 135
To count the number of times some property is the same, you could loop through the array and do something like this:
let arr = [
{
id: 1,
name: "Joe"
},
{
id: 1,
name: "Joe"
},
{
id: 2,
name: "Mary"
}
];
let idCounts = {};
const propertyToCount = 'id';
for (let i = 0; i < arr.length; i++) {
const elem = arr[i];
if(typeof idCounts[elem[propertyToCount]] === 'undefined') {
idCounts[elem[propertyToCount]] = 1;
}
else {
idCounts[elem[propertyToCount]] = idCounts[elem[propertyToCount]] + 1;
}
}
I hope I understood the question correctly.
Upvotes: 1