Попробуйте сгруппировать элементы по методу reduce
, а затем просто filter
let hashMap = input.reduce((a, {workType, priority})=> {
if (priority > 0) {
a[priority] = a[priority] || [];
a[priority].push(workType);
}
return a;
},{});
let result = Object.entries(hashMap)
.filter(([k, v]) => v.length > 1)
.map(([k, v]) => v);
Пример:
let input = [{ "workType": "NDB To Nice", "priority": 5 },
{ "workType": "PDAD", "priority": 0 },
{ "workType": "PPACA", "priority": 0 },
{ "workType": "Retrigger", "priority": "5" },
{ "workType": "Special Intake Request Intake", "priority": "7" }
];
// I need to call out all the "worktype" which have same "priority" , if the priority is NOT EQUAL TO 0.
// keys = ["NDB To Nice","Retrigger"]
let hashMap = input.reduce((a, {workType, priority})=> {
if (priority > 0) {
a[priority] = a[priority] || [];
a[priority].push(workType);
}
return a;
},{});
let result = Object.entries(hashMap).filter(([k, v]) => v.length > 1).map(([k, v]) => v);
console.log(result);
Или вам нравятся встроенные методы (на мой взгляд, ему не хватает читабельности)
let input = [{ "workType": "NDB To Nice", "priority": 5 },
{ "workType": "PDAD", "priority": 0 },
{ "workType": "PPACA", "priority": 0 },
{ "workType": "Retrigger", "priority": "5" },
{ "workType": "Special Intake Request Intake", "priority": "7" }
];
let hashMap = input.reduce((a, {workType, priority})=> ( ((priority > 0) ?
(a[priority] = a[priority] || [], a[priority].push(workType)) : null), a), {});
let result = Object.entries(hashMap).filter(([k, v]) => v.length > 1).map(([k, v]) => v)
console.log(result);