Home > database >  How to check whether an array contain/include an object property in another array JavaScript
How to check whether an array contain/include an object property in another array JavaScript

Time:12-23

I have an array known as VotersDetails and was set into the local storage when user registered by clicking register button with function register() .

I also have an object called voters with properties like Fname: Lname: County: PhoneNumber etc.

The object are set into the array, each having their index No.

So also i have another button called finish with the function finish() that also set some object property into another array called allElectionResult

My question now is that I want to know how to check using if statements if for example the email the user entered when he/she want to login is matched with what he registered with and also included in the last array known as allElectionResult.

var votersDetails = [];

if (localStorage.localVoters) {
  var oldVoters = JSON.parse(localStorage.getItem("localVoters"));
  votersDetails = oldVoters;
}

function submitRegistration() {
  if (firstName.value == "") {
    firstName.style.borderColor = "red";
  } else {
    var voters = {
      //passport : photo.value,
      fname: firstName.value,
      lname: lastName.value,
      nin: nin.value,
      country: countryOption.value,
      state: stateOption.value,
      email: email.value,
      phonenumber: phoneNumber.value,
      dateofbirth: dateOfBirth.value,
      gender: genderOption.value,
      password: pass.value,
      check: invalidCheck.value,
      id: Math.floor(Math.random() * 10000000000),
      key: Math.floor(Math.random() * 1000000),
    };
    votersDetails.push(voters);
    localStorage.setItem("localVoters", JSON.stringify(votersDetails));
    showPass();
  }
}

function signIn() {
  var votersId = loginId.value;
  var votersKey = pass.value;
  var found = false;
  for (let index = 0; index < votersDetails.length; index  ) {
    if (
      (votersDetails[index].id || votersDetails[index].email == votersId) &&
      votersDetails[index].key == votersKey
    ) {
      found = true;
      break;
    }
  }

  if (found == true && allElectionResult.includes('myEmail')) {
    alert("Yes")
    //window.location.href = "e-voting-votingPage.html";
  } else if (found == true && (!allElectionResult.includes('myEmail'))) {
    alert("No")
  } else {
    alert("Incorrect details, Kindly please check what you enter and re-type");
  }
}

function finish() {
  for (let index = 0; index < votersDetails.length; index  ) {
    disp.innerHTML = `${votersDetails[index].fname}`
    disp3.innerHTML = `${votersDetails[index].state}`
    disp4.innerHTML = `${votersDetails[index].email}`
  }

  var allVotersElectionResult = {
    // allResult: electionResult,
    name: disp.innerHTML,
    myState: disp3.innerHTML,
    myEmail: disp4.innerHTML,
  }

  allElectionResult.push(allVotersElectionResult);
  localStorage.setItem("localResultsAll", JSON.stringify(allElectionResult));

  window.location.href = "final.html";
}

CodePudding user response:

Store the email which is entered by user into a variable.

let email = "[email protected]";

Matching with votersDetails

let foundEmailOnVotersDetails = votersDetails.find((voter) => {
  return email === voter.myEmail;
})

Matching with allElectionResult

let foundEmailOnElectionResult = allElectionResult.find((voter) => {
  return email === voter.email
})

CodePudding user response:

Please check the below signIn function

function signIn() {
  var votersId = loginId.value;
  var votersKey = pass.value;
  var found = false;
  for (let index = 0; index < votersDetails.length; index  ) {
    if (
      (votersDetails[index].id || votersDetails[index].email == votersId) &&
      votersDetails[index].key == votersKey
    ) {
      found = true;
      break;
    }
  }

  // logic to check whether the User entered email is in allElectionResult array
  let foundInAllElectionResult = false;
  for(let user of allElectionResult) {
    if(user.myEmail === votersId) {
      foundInAllElectionResult = true;
      break;
    }
  }

  if (found == true && foundInAllElectionResult) {
    alert("Yes")
    //window.location.href = "e-voting-votingPage.html";
  } else if (found == true && (!foundInAllElectionResult)) {
    alert("No")
  } else {
    alert("Incorrect details, Kindly please check what you enter and re-type");
  }
}

includes() checks whether an element is present in the array or not, NOTE If you are searching a data in array of objects, you should not use includes(), you should do

  1. Loop through the array to find the element.

  2. If found, alter the flag variable, then check for that variable

You could also use array method like findIndex and check whether the element is in array or not to achieve this.

  • Related