I'm trying to compare an object with an array of objects to see if it matches exactly to one of the objects within the array.
The user enters their address in a form, using a postcode lookup, and the form inputs are converted into a Javascript object. I need to then compare this address object with a list of address objects that are in an array. There needs to be an exact match between the form inputs object and the array in order to proceed.
I've tried to use include() but it doesn't seem to be working and I'm struggling to find an alternative.
Here is my code so far:
var eligiblePCVoucher = [{
"postcode": "AA90 4XX",
"company": "",
"address1": "1 Seaview Road",
"address2": "Smalltown-on-Sea",
"town": "Smalltown",
"county": "Norfolk"
},
{
"postcode": "AA90 4XX",
"company": "",
"address1": "2 Seaview Road",
"address2": "Smalltown-on-Sea",
"town": "Smalltown",
"county": "Norfolk"
},
{
"postcode": "AA90 4XX",
"company": "",
"address1": "2 Seaview Road",
"address2": "Smalltown-on-Sea",
"town": "Smalltown",
"county": "Norfolk"
},
{
"postcode": "AA90 4XX",
"company": "",
"address1": "Seashore",
"address2": "Seaview Road",
"town": "Smalltown",
"county": "Norfolk"
},
{
"postcode": "AA90 4XX",
"company": "",
"address1": "Bayview",
"address2": "Seaview Road",
"town": "Smalltown",
"county": "Norfolk"
}
]
/* POSTCODE LOOKUP */
document.getElementById("dropdown").addEventListener('change', function() {
var pc_result = document.getElementsByClassName("pc_row");
var i;
for (i = 0; i < pc_result.length; i++) {
pc_result[i].style.display = 'table';
}
})
/* MY FUNCTION */
document.getElementById('submit_eligible').addEventListener('click', function() {
var postCode = document.getElementById('postcode').value;
var addressOne = document.getElementById('address1').value;
var addressTwo = document.getElementById('address2').value;
var town = document.getElementById('town').value;
var county = document.getElementById('county').value;
var fullAddress = addressOne + " " + addressTwo + " " + postCode;
let form = document.querySelector('#post');
let data = new FormData(form);
var object = {};
data.forEach(function(value, key) {
object[key] = value;
});
var addressObj = JSON.stringify(object);
console.log(addressObj);
console.log(eligiblePCVoucher);
console.log(eligiblePCVoucher.includes(addressObj));
})
<form id="post">
<label for="postcode">Postcode</label></td>
<input type="text" value="" id="postcode" name="postcode" /></td>
<input type="submit" value="lookup" id="lookup" onclick="SearchBegin();return false;" />
<select id="dropdown" style='display:none;' onchange="SearchIdBegin()">
<option>Select an address:</option>
</select>
<label for="company">Company</label></td>
<input type="text" value="" id="company" name="company" /></td>
<label for="address1">Address 1</label></td>
<input type="text" value="" id="address1" name="address1" /></td>
<label for="address2">Address 2</label></td>
<input type="text" value="" id="address2" name="address2" /></td>
<label for="town">Town</label></td>
<input type="text" value="" id="town" name="town" /></td>
<label for="county">County</label></td>
<input type="text" value="" id="county" name="county" /></td>
<input type="submit" value="Am I eligible?" id="submit_eligible" />
</form>
Thanks in advance.