如何根据反应js中的两个条件显示元素总数?
How to show total count of elements based on two conditions in react js?
我试图根据两个条件显示参与者总数:"Nume": "4S110"
和 "Nume": "Contabilitate"
来自 INDICATORI
数组和 Cursuri
数组。
将它们显示为
const cont = popular.reduce((total, participant) => total + participant.Cursuri
.filter(curs => curs.Nume === "Contabilitate") && participant.INDICATORI
.filter(indicator => indicator.Nume === "4S110").length,0)
output is : 0, it should be 1
api is:
```
"rows": [
{
"id": "15f806ec-79cf-498f-8a4d-8bc8fdf8c43e",
"Nume": "negrea",
"Prenume": "ioana",
"Varsta": 40,
"createdAt": "2022-04-27T13:17:05.000Z",
"updatedAt": "2022-04-27T13:17:05.000Z",
"deletedAt": null,
"INDICATORI": [
{
"id": "068170b3-7995-41df-8fac-4f2bc577e2c6",
"Nume": "4S110",
"importHash": null,
}
],
"Cursuri": [{
"id": "068170b3-7995-41df-8fac-4f2bc577e2c6",
"Nume": "Contabilitate",
"importHash": null,
]
},
.filter()
函数returns一个数组所以需要添加.length
。 Math.min()
如果您只想对这样的人计数一次(必须在两项中都有一个条目),可以提供帮助。
let popular = JSON.parse(`[
{
"id": "15f806ec-79cf-498f-8a4d-8bc8fdf8c43e",
"Nume": "negrea",
"Prenume": "ioana",
"Varsta": 40,
"createdAt": "2022-04-27T13:17:05.000Z",
"updatedAt": "2022-04-27T13:17:05.000Z",
"deletedAt": null,
"INDICATORI": [
{
"id": "068170b3-7995-41df-8fac-4f2bc577e2c6",
"Nume": "4S110",
"importHash": null
}
],
"Cursuri": [{
"id": "068170b3-7995-41df-8fac-4f2bc577e2c6",
"Nume": "Contabilitate",
"importHash": null
}]
}]`);
const count = popular.reduce((total, participant) => total + Math.min(participant.Cursuri
.filter(indicator => indicator.Nume === "Contabilitate").length, participant.INDICATORI
.filter(indicator => indicator.Nume === "4S110").length), 0)
console.log(count);
我试图根据两个条件显示参与者总数:"Nume": "4S110"
和 "Nume": "Contabilitate"
来自 INDICATORI
数组和 Cursuri
数组。
将它们显示为
const cont = popular.reduce((total, participant) => total + participant.Cursuri
.filter(curs => curs.Nume === "Contabilitate") && participant.INDICATORI
.filter(indicator => indicator.Nume === "4S110").length,0)
output is : 0, it should be 1
api is:
```
"rows": [
{
"id": "15f806ec-79cf-498f-8a4d-8bc8fdf8c43e",
"Nume": "negrea",
"Prenume": "ioana",
"Varsta": 40,
"createdAt": "2022-04-27T13:17:05.000Z",
"updatedAt": "2022-04-27T13:17:05.000Z",
"deletedAt": null,
"INDICATORI": [
{
"id": "068170b3-7995-41df-8fac-4f2bc577e2c6",
"Nume": "4S110",
"importHash": null,
}
],
"Cursuri": [{
"id": "068170b3-7995-41df-8fac-4f2bc577e2c6",
"Nume": "Contabilitate",
"importHash": null,
]
},
.filter()
函数returns一个数组所以需要添加.length
。 Math.min()
如果您只想对这样的人计数一次(必须在两项中都有一个条目),可以提供帮助。
let popular = JSON.parse(`[
{
"id": "15f806ec-79cf-498f-8a4d-8bc8fdf8c43e",
"Nume": "negrea",
"Prenume": "ioana",
"Varsta": 40,
"createdAt": "2022-04-27T13:17:05.000Z",
"updatedAt": "2022-04-27T13:17:05.000Z",
"deletedAt": null,
"INDICATORI": [
{
"id": "068170b3-7995-41df-8fac-4f2bc577e2c6",
"Nume": "4S110",
"importHash": null
}
],
"Cursuri": [{
"id": "068170b3-7995-41df-8fac-4f2bc577e2c6",
"Nume": "Contabilitate",
"importHash": null
}]
}]`);
const count = popular.reduce((total, participant) => total + Math.min(participant.Cursuri
.filter(indicator => indicator.Nume === "Contabilitate").length, participant.INDICATORI
.filter(indicator => indicator.Nume === "4S110").length), 0)
console.log(count);