1

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.

isherwood
  • 58,414
  • 16
  • 114
  • 157
NewMike
  • 30
  • 6
  • 2
    I don't think `includes` will work since `{} === {}` is false. The objects need to be the same object (not just have identical properties). – evolutionxbox Sep 22 '21 at 15:55
  • Given all the postcodes in your array are `AA90 4XX` what would you expect to happen if I entered that postcode? Where is your `SearchBegin` method? – Jamiec Sep 22 '21 at 16:08

1 Answers1

2

You can check if the object is present in the array using the Array.some() method (ref).

const is_present = eligiblePCVoucher.some((e) => {
  return Object.entries(e).toString() === Object.entries(addressObj).toString();
});

What this will do is concatenate the key and value of each of the properties in an object and do an exact string comparison. Some() will return true only if at least an object matches

isherwood
  • 58,414
  • 16
  • 114
  • 157