Home > Net >  Object with users, remove all but one when 2 values match (firstname, lastname)
Object with users, remove all but one when 2 values match (firstname, lastname)

Time:10-18

I want to create a new object, where the users should only appear once. So if a person appears multiple times, which is given when given_name and family_name both match, all but one are removed.

Example:

 [
      {
        given_name: 'Aha',
        family_name: 'Yes',
        email: '[email protected]',
      },
      {
        given_name: 'Testname',
        family_name: 'Test',
        email: '[email protected]',
      },
     {
        given_name: 'Hans',
        family_name: 'Test',
        email: '[email protected]',
      },
     {
        given_name: 'Aha',
        family_name: 'Yes',
        email: '[email protected]',
      }
    ]

So the above should result in this:

[
      {
        given_name: 'Aha',
        family_name: 'Yes',
        email: '[email protected]',
      },
      {
        given_name: 'Testname',
        family_name: 'Test',
        email: '[email protected]',
      },
     {
        given_name: 'Hans',
        family_name: 'Test',
        email: '[email protected]',
      }
    ]

CodePudding user response:

using ES6

let arry=[
      {
        given_name: 'Aha',
        family_name: 'Yes',
        email: '[email protected]',
      },
      {
        given_name: 'Testname',
        family_name: 'Test',
        email: '[email protected]',
      },
     {
        given_name: 'Hans',
        family_name: 'Test',
        email: '[email protected]',
      },
     {
        given_name: 'Aha',
        family_name: 'Yes',
        email: '[email protected]',
      }
    ]
    
    let result= arry.filter((item, index, self) =>
  index === self.findIndex((t) => (
    t.given_name === item.given_name && t.family_name === item.family_name
  ))
)

console.log(result)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Please use the below one liner code

let arr = arr.filter((item,index)=>arr.findIndex(item2=>(item.family_name == item2.family_name && item.given_name == item2.given_name))>=index)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can do something like this, here we are using a map to get the unique values. For the keys in the map, we use a combination of given_name and family_name.

const users = [{given_name: "Aha", family_name: "Yes", email: "[email protected]"}, {given_name: "Testname", family_name: "Test", email: "[email protected]"}, {given_name: "Hans", family_name: "Test", email: "[email protected]"}, {given_name: "Aha", family_name: "Yes", email: "[email protected]"}];

const removeDuplicates = (data) => {
   const userNameMap = {};
   data.forEach((user) => {
       const userName = `${user.given_name}|${user.family_name}`;
       userNameMap[userName] = user;
   });

   return Object.values(userNameMap);
};

console.log(removeDuplicates(users));
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related