I am currently working on a project in Vue where I need to match the initial part of a UK postcode with the data stored in a JSON file.
So far, I have been successful in matching postcodes that begin with 2 letters. However, some UK postcodes may only start with one letter, causing my current method to fail.
If you want to see the full code, you can visit this link: https://codesandbox.io/s/48ywww0zk4
Here is a snippet from the JSON file:
{
"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
);
I could use some assistance in correctly identifying 'B' for a postcode like B94 5RD and 'BA' for BA33HT. Can anyone help me with this?