![](/img/trans.png)
[英]How to check whether an array of object is present in another array of object in Javascript
[英]How to check whether an array contain/include an object property in another array JavaScript
我有一个名为 VotersDetails 的数组,当用户通过单击带有函数 register() 的注册按钮进行注册时,它被设置到本地存储中。
我还有一个名为 voters 的对象,它具有 Fname: Lname: County: PhoneNumber 等属性。
对象被设置到数组中,每个对象都有它们的索引号。
所以我还有另一个名为 finish 的按钮,其函数 finish() 还将一些对象属性设置到另一个名为 allElectionResult 的数组中
我现在的问题是,我想知道如何使用 if 语句来检查,例如,如果用户在他/她想要登录时输入的电子邮件与他注册的电子邮件相匹配,并且还包含在称为 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";
}
你能用更短的问题来描述确切的问题吗?
将用户输入的电子邮件存储到变量中。
let email = "langesh@gmail.com";
与选民匹配详情
let foundEmailOnVotersDetails = votersDetails.find((voter) => {
return email === voter.myEmail;
})
与 allElectionResult 匹配
let foundEmailOnElectionResult = allElectionResult.find((voter) => {
return email === voter.email
})
请检查以下signIn
功能
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()
检查一个元素是否存在于数组中,注意如果你在对象数组中搜索数据,你不应该使用includes()
,你应该这样做
遍历数组以查找元素。
如果找到,更改标志变量,然后检查该变量
您还可以使用findIndex之类的数组方法并检查元素是否在数组中以实现此目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.