Home > Software engineering >  Comparing 2 arrays, then push if the condition is met
Comparing 2 arrays, then push if the condition is met

Time:06-02

I am trying to filter out emails that are already existing in userData, but my code keeps pushing same data over and over as long as the email is not the same.

Here's my code:

userData:[ {email: "[email protected]", 
           first_name: "Jane", 
           last_name: "Doe"}, 
           {email: "[email protected]", 
           first_name: "john", 
           last_name: "doe"}, 
           {email: "[email protected]", 
           first_name: "Mary", 
           last_name: "Jane"}
],
parseData:[ {email: "[email protected]", 
           first_name: "Jane", 
           last_name: "Doe"}, 
           {email: "[email protected]", 
           first_name: "Johnny", 
           last_name: "Bravo"}, 
           {email: "[email protected]", 
           first_name: "Jayson", 
           last_name: "Abuela"}
],
newData: []

var userData = this.userData
var parsedData = this.parseData
function(results) {
            const parsedData = results.data
            for(var j = 0; j < parsedData.length; j  ){
              userData.map((data)=>{
                  if(data.email.toLowerCase() != parseData[j].email.toLowerCase()){
                    newData.push(parsedData[j])
                    
                  }else{
                     alert("This " parsedData[j].email " already exist.")
                   }
              })
            }
          }

I am expecting user with email of [email protected] and [email protected] gets push in my newData, it does gets pushed for like 5 times each data. I just want it to be pushed once.

CodePudding user response:

Can't you just do it the simple way?

const userData = [ {email: "[email protected]", 
           first_name: "Jane", 
           last_name: "Doe"}, 
           {email: "[email protected]", 
           first_name: "john", 
           last_name: "doe"}, 
           {email: "[email protected]", 
           first_name: "Mary", 
           last_name: "Jane"}
];

const parseData = [ {email: "[email protected]", 
           first_name: "Jane", 
           last_name: "Doe"}, 
           {email: "[email protected]", 
           first_name: "Johnny", 
           last_name: "Bravo"}, 
           {email: "[email protected]", 
           first_name: "Jayson", 
           last_name: "Abuela"}
];

const tempEmails = userData.map(x => x.email.toLowerCase());
const newEmails = parseData.filter(x => !tempEmails.includes(x.email.toLowerCase()));

console.log(newEmails);

Couldn't figure out which way around you want it. You can just swap parseData and userData if this isn't the right way around.

Edit: These are called lambda functions. They're very handy and simple. I'm just using map() to create an array of just the email strings. Then I use filter() to check each object in the array and if the email key is included in that first map of email strings, exclude it. Simple stuff.

CodePudding user response:

Your code doesn't test that the email isn't in the other array. It's comparing with each element of the other array, and pushing for every one that doesn't match.

Create a Set containing all the emails in parsedData. Then you can use the .has() method to test if the email is already in there.

const parsedDataSet = new Set(parsedData.map(({email}) => email.toLowerCase());
userData.forEach(u => {
    if (!parsedDataSet.has(u.email.toLowerCase())) {
        parsedData.push(u);
        parsedDataSet.add(u.email.toLowerCase());
    }
);
  • Related