过滤具有相同ID的对象,而不是javascript .filter()

时间:2019-03-06 22:49:05

标签: javascript reactjs react-native

我需要在数组中进行过滤,还需要有一个计数器来知道它具有相同ID的记录数。我尝试使用过滤器,但由于不知道将其与之比较,所以没有得到它。它是一个学生列表,只有3种不同的列表,并且会重复多次,因为它是为学生重复使用的药品列表。

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)

1 个答案:

答案 0 :(得分:1)

要计算某些属性相同的次数,可以遍历数组并执行以下操作:

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;
    }
}

我希望我正确理解了这个问题。