1

I have an API which has a response like this

{conditions:{,…}
individuals:[{employee_id: 12300, employee_type: "Permanent", person_id: "1211211", full_name: "John Jacobs",…},…]
page_num:5
rows:10
total_count:213}

Each entry inside the individuals array looks like this

[ { employee_id: 12300,
    employee_type: 'Permanent',
    person_id: '1211211',
    full_name: 'John Jacobs',
    email_id: 'john_jacobs@gmail.com',
    person_role: [ 
    {rg_code: "AP",
    cl_code: "12",
    data : "South East Asia",
    loc_code : "IN"},

    {rg_code: "CD",
    cl_code: "15",
    data : "Middle East Asia",
    loc_code : "QY"},

    {rg_code: "AP",
    cl_code: "12",
    data : "South East Asia",
    loc_code : "IN"},

    {rg_code: "DF",
    cl_code: "34",
    data : "South East Europe",
    loc_code : "FR"}
     ],
    staffings: [ {id: 1244,
    ind_id: 113322,
    p_id : 112,
    p_name: "Bollywood"},

     {id: 1245,
    ind_id: 112322,
    p_id : 113,
    p_name: "Tollywood"},
    ],
    first_name: 'John',
    last_name: 'Jacobs',
    location:
     { country: 'India',
       region: 'South Asia',
       code: 'SA/IN',
       name: 'Bangalore' },
    assistants: [ {} ],
    job_title: 'SSE-2',
    person_full_name: 'John Jacobs'}
     ]

I'm trying to find all entries inside the individuals array that have duplicate entries of same loc_code inside person_role - for eg - in the example entry given below there are two entries for loc_code = 'IN'. Is the solution possible without use of for loops and only with the use of filter and reduce methods?

demouser123
  • 4,108
  • 9
  • 50
  • 82

2 Answers2

0

You can apply filter and then reduce to construct a Set of the loc_codes for that person. If the set has the same number of values than the original number of persons, that means they are all different. The performance can be improved, as it walks the whole person_role array even when it could stop at the first duplicate.

var filtered = individuals.filter(el => {
    return el.person_role.reduce( (prev, cur) => { 
        return prev.add(cur.loc_code);
    }, new Set() ).size !== el.person_role.length;
});


console.log(filtered);
0

You can compare the size of unique items with the total size. My solution involves doing it with reduce and extracting a filtering functionality into a separate function:

function hasDuplicateLoc({ person_role }) {

    const uniq = person_role.reduce((all, {loc_code}) => {

        if (!all.includes(loc_code)) all.push(loc_code);
        return all;

    }, []);

    return person_role.length !== uniq.length;

}

const result = individuals.filter(hasDuplicateLoc);

console.log(result);
Leonid Pyrlia
  • 1,594
  • 2
  • 11
  • 14