I'm trying to match the first part of a UK postcode to those that I have held in a JSON file. I'm doing this in Vue.
At the moment I have managed to match the postcode if it has 2 letters that match, but some UK postcodes do not start with 2 letters, some just have the one and this is where it fails.
See here for full code https://codesandbox.io/s/48ywww0zk4
Sample of JSON
{
"id": 1,
"postcode": "AL",
"name": "St. Albans",
"zone": 3
},
{
"id": 2,
"postcode": "B",
"name": "Birmingham",
"zone": 2
},
{
"id": 3,
"postcode": "BA",
"name": "Bath",
"zone": 5
}
let postcodeZones = this.postcodeDetails.filter(
pc => pc.postcode
.toLowerCase()
.slice(0, 2)
.indexOf(this.selectPostcode.toLowerCase().slice(0, 2)) > -1
);
Can anyone help me find (for example) 'B' if I type B94 5RD & 'BA' if I type BA33HT?