store_id의 개수로 필터링하려는 객체 배열이 있다고 가정 해 보겠습니다.
[{
ingredient_id: "lim1",
status: true,
store_id: "1"
},
{
ingredient_id: "lem1",
status: true,
store_id: "1"
},
{
ingredient_id: "lem1",
status: true,
store_id: "5"
}]
x의 값이 2이면 내가 원하는 출력은 다음과 같습니다.
[{
ingredient_id: "lim1",
status: true,
store_id: "1"
},
{
ingredient_id: "lem1",
status: true,
store_id: "1"
}]
store_id : '1'이 두 번 나타나기 때문에
그러나 x의 값이 3이면 빈 값을 반환하고 싶습니다.
내가 답을 찾은 모든 곳에서 중복을 반환하는 대신 중복을 제거하는 방법만을 보여주고 그 반대의 시도는 도움이되지 않았습니다.
각 store_id
값 의 개수가있는 객체를 생성 한 다음 각 항목의 개수가 값 store_id
과 일치 하는지 여부에 대해 입력 배열을 필터링 하여이 문제를 해결할 수 있습니다 x
.
const data = [{
ingredient_id: "lim1",
status: true,
store_id: "1"
},
{
ingredient_id: "lem1",
status: true,
store_id: "1"
},
{
ingredient_id: "lem1",
status: true,
store_id: "5"
}
];
const counts = data.reduce((c, v) => {
c[v.store_id] = (c[v.store_id] || 0) + 1;
return c;
}, {});
const x = 2;
const result = data.filter(v => counts[v.store_id] == x);
console.log(result);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다