I'm in need of some assistance with a problem that I've been struggling with. I have several JavaScript objects containing different data pieces as shown below:-
Object {id: 1, shopcounty: "cornwall", shopaddress: "the cycle centre,<br />1 new street,<br />penzance,<br />tr18 2lz<br />}
Object {id: 2, shopcounty: "cornwall", shopaddress: "heliport link road,<br />long rock,<br />bude,<br />tr18 3rg"}
Object {id: 3, shopcounty: "cornwall", shopaddress: "heliport link road,<br />long rock,<br />bude,<br />tr18 3rg"}
Object {id: 4, shopcounty: "cornwall", shopaddress: "west end cottage,<br />trescowe,<br />penzance,<br /&>tr20 9rn"}
Object {id: 5, shopcounty: "cornwall", shopaddress: "22 joannies watch,<br />saint ives,<br />tr26 2fr"}
What I'd like to achieve is to take a user input value and search the address of each object for a matching string, then return all details if there's a match.
For instance, if a user types in "bude" as their location, objects 2 and 3 should be returned along with their respective data. However, my current code seems to just return true for every object. I've tried using methods like match() and indexOf(), but still end up with all objects being returned.
<input id="submit" type="submit" name="form-submit">
// user input: "bude, united kingdom"
<script>
$('#submit').on('click tap', function(e){
e.preventDefault();
var userInput = document.getElementById('user-location').value;
for (var i = 0; i < bikeshops.length; i++) {
console.log(bikeshops[i]);
if($.inArray(userInput, bikeshops[i])){
// console.log(bikeshops[i].shopaddress);
// returns everything!
}
}
});
</script>