[Vuejs]-Searching through JSON result in Vue

0πŸ‘

βœ…

In:

"address_components" : [
     ...
     {
        "long_name" : "Stockholm",
        "short_name" : "Stockholm",
        "types" : [ "postal_town" ]
     },

types is an array, to look for values in it, use types.includes(value), not ==.

Also, returning in .forEach() does not do what you think it does. Since you are looking for a value inside the address_components array, your best bet is to use .find() instead of .forEach():

method(place) {
  let placeFound = place.address_components.find(m => m.types.includes("postal_town"));
  if (placeFound) {
    return placeFound.long_name;
  }
  return "I f**ked up!";
}

Demo JSFiddle: https://jsfiddle.net/acdcjunior/50wL7mdz/148305/

Leave a comment