1

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)

3
  • Just to clarify, you're looking for all items with duplicate IDs, not just items matching a specific ID? Commented Mar 6, 2019 at 22:52
  • I don't see anything named id,.. what property is meant to be the id? Commented Mar 6, 2019 at 23:11
  • exactly, I want to join all items with the same id, and also show how many times each was repeated, the field to use with id is the co90Alun. Commented Mar 7, 2019 at 23:33

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.