0

Long story short, array's length depends on the selected city so I can't select the element using an index because I can't know what index would the element have. Therefore I somehow need to select it based on the value of one of its properties.

In my case, I want to select the element which contains the object that has property types: ['locality', 'political'].

Example results. I haven't included the first 7 array elements since they are not relevent:

7:
    address_components: (3) [{…}, {…}, {…}]
    types: Array(2)
        0: "locality"
        1: "political"
8:
    address_components: (2) [{…}, {…}]
    types: Array(2)
        0: "administrative_area_level_1"
        1: "political"

As you can see, every element has a types property which is an array. I'm currently trying to select element with index 7 using the types: ['locality', 'political'] and not the index.

Sadly I have no clue how to do that so I haven't supplemented any code.

[
  {
    "address_components": [
      {
        "long_name": "19",
        "short_name": "19",
        "types": [
          "street_number"
        ]
      },
      {
        "long_name": "Clanbrassil Street Lower",
        "short_name": "Clanbrassil Street Lower",
        "types": [
          "route"
        ]
      },
      {
        "long_name": "Wood Quay",
        "short_name": "Wood Quay",
        "types": [
          "neighborhood",
          "political"
        ]
      },
      {
        "long_name": "Dublin 8",
        "short_name": "Dublin 8",
        "types": [
          "postal_town"
        ]
      },
      {
        "long_name": "County Dublin",
        "short_name": "County Dublin",
        "types": [
          "administrative_area_level_1",
          "political"
        ]
      },
      {
        "long_name": "Ireland",
        "short_name": "IE",
        "types": [
          "country",
          "political"
        ]
      },
      {
        "long_name": "D08 X798",
        "short_name": "D08 X798",
        "types": [
          "postal_code"
        ]
      }
    ],
    "formatted_address": "19 Clanbrassil Street Lower, Wood Quay, Dublin, D08 X798, Ireland",
    "geometry": {
      "location": {
        "lat": 53.3349244,
        "lng": -6.2740099
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "northeast": {
          "lat": 53.3362733802915,
          "lng": -6.272660919708499
        },
        "southwest": {
          "lat": 53.3335754197085,
          "lng": -6.275358880291503
        }
      }
    },
    "place_id": "ChIJZckoE7wNZ0gRgHdbQYYvYTk",
    "plus_code": {
      "compound_code": "8PMG+X9 Dublin, County Dublin, Ireland",
      "global_code": "9C5M8PMG+X9"
    },
    "types": [
      "cafe",
      "establishment",
      "food",
      "point_of_interest"
    ]
  },
  {
    "address_components": [
      {
        "long_name": "21",
        "short_name": "21",
        "types": [
          "street_number"
        ]
      },
      {
        "long_name": "R137",
        "short_name": "R137",
        "types": [
          "route"
        ]
      },
      {
        "long_name": "Wood Quay",
        "short_name": "Wood Quay",
        "types": [
          "neighborhood",
          "political"
        ]
      },
      {
        "long_name": "Dublin 8",
        "short_name": "Dublin 8",
        "types": [
          "postal_town"
        ]
      },
      {
        "long_name": "County Dublin",
        "short_name": "County Dublin",
        "types": [
          "administrative_area_level_1",
          "political"
        ]
      },
      {
        "long_name": "Ireland",
        "short_name": "IE",
        "types": [
          "country",
          "political"
        ]
      }
    ],
    "formatted_address": "21 R137, Wood Quay, Dublin, Ireland",
    "geometry": {
      "location": {
        "lat": 53.3348735,
        "lng": -6.273865
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "northeast": {
          "lat": 53.3362224802915,
          "lng": -6.272516019708497
        },
        "southwest": {
          "lat": 53.3335245197085,
          "lng": -6.275213980291502
        }
      }
    },
    "place_id": "ChIJYVO9ECIMZ0gR2bQrf0Ktmso",
    "plus_code": {
      "compound_code": "8PMG+WF Dublin, County Dublin, Ireland",
      "global_code": "9C5M8PMG+WF"
    },
    "types": [
      "street_address"
    ]
  },
  {
    "address_components": [
      {
        "long_name": "18",
        "short_name": "18",
        "types": [
          "street_number"
        ]
      },
      {
        "long_name": "Clanbrassil Street Lower",
        "short_name": "Clanbrassil Street Lower",
        "types": [
          "route"
        ]
      },
      {
        "long_name": "Wood Quay",
        "short_name": "Wood Quay",
        "types": [
          "neighborhood",
          "political"
        ]
      },
      {
        "long_name": "Dublin 8",
        "short_name": "Dublin 8",
        "types": [
          "postal_town"
        ]
      },
      {
        "long_name": "County Dublin",
        "short_name": "County Dublin",
        "types": [
          "administrative_area_level_1",
          "political"
        ]
      },
      {
        "long_name": "Ireland",
        "short_name": "IE",
        "types": [
          "country",
          "political"
        ]
      }
    ],
    "formatted_address": "18 Clanbrassil Street Lower, Wood Quay, Dublin, Ireland",
    "geometry": {
      "location": {
        "lat": 53.3349987,
        "lng": -6.2739341
      },
      "location_type": "RANGE_INTERPOLATED",
      "viewport": {
        "northeast": {
          "lat": 53.3363476802915,
          "lng": -6.272585119708499
        },
        "southwest": {
          "lat": 53.3336497197085,
          "lng": -6.275283080291502
        }
      }
    },
    "place_id": "EiwxOCBDbGFuYnJhc3NpbCBTdHJlZXQgTG93ZXIsIER1YmxpbiwgSXJlbGFuZCIaEhgKFAoSCQexJhIiDGdIEUlWW2rVpRfqEBI",
    "types": [
      "street_address"
    ]
  },
  {
    "address_components": [
      {
        "long_name": "Clanbrassil Street Lower",
        "short_name": "R137",
        "types": [
          "route"
        ]
      },
      {
        "long_name": "Merchants Quay",
        "short_name": "Merchants Quay",
        "types": [
          "neighborhood",
          "political"
        ]
      },
      {
        "long_name": "Dublin 8",
        "short_name": "Dublin 8",
        "types": [
          "postal_town"
        ]
      },
      {
        "long_name": "County Dublin",
        "short_name": "County Dublin",
        "types": [
          "administrative_area_level_1",
          "political"
        ]
      },
      {
        "long_name": "Ireland",
        "short_name": "IE",
        "types": [
          "country",
          "political"
        ]
      }
    ],
    "formatted_address": "Clanbrassil Street Lower, Merchants Quay, Dublin, Ireland",
    "geometry": {
      "bounds": {
        "northeast": {
          "lat": 53.33516729999999,
          "lng": -6.273907599999999
        },
        "southwest": {
          "lat": 53.3345896,
          "lng": -6.274462
        }
      },
      "location": {
        "lat": 53.3348778,
        "lng": -6.274183000000001
      },
      "location_type": "GEOMETRIC_CENTER",
      "viewport": {
        "northeast": {
          "lat": 53.33622743029149,
          "lng": -6.272835819708497
        },
        "southwest": {
          "lat": 53.33352946970849,
          "lng": -6.275533780291502
        }
      }
    },
    "place_id": "ChIJzSxnESIMZ0gR8A77SK0ccBQ",
    "types": [
      "route"
    ]
  },
  {
    "address_components": [
      {
        "long_name": "Wood Quay",
        "short_name": "Wood Quay",
        "types": [
          "neighborhood",
          "political"
        ]
      },
      {
        "long_name": "Dublin 8",
        "short_name": "Dublin 8",
        "types": [
          "postal_town"
        ]
      },
      {
        "long_name": "County Dublin",
        "short_name": "County Dublin",
        "types": [
          "administrative_area_level_1",
          "political"
        ]
      },
      {
        "long_name": "Ireland",
        "short_name": "IE",
        "types": [
          "country",
          "political"
        ]
      }
    ],
    "formatted_address": "Wood Quay, Dublin, Ireland",
    "geometry": {
      "bounds": {
        "northeast": {
          "lat": 53.3454641,
          "lng": -6.2680769
        },
        "southwest": {
          "lat": 53.329641,
          "lng": -6.275519999999999
        }
      },
      "location": {
        "lat": 53.33275459999999,
        "lng": -6.2706881
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "northeast": {
          "lat": 53.3454641,
          "lng": -6.2680769
        },
        "southwest": {
          "lat": 53.329641,
          "lng": -6.275519999999999
        }
      }
    },
    "place_id": "ChIJl78TlSEMZ0gRUHtT8SkTqVA",
    "types": [
      "neighborhood",
      "political"
    ]
  },
  {
    "address_components": [
      {
        "long_name": "Dublin 8",
        "short_name": "Dublin 8",
        "types": [
          "postal_town"
        ]
      },
      {
        "long_name": "County Dublin",
        "short_name": "County Dublin",
        "types": [
          "administrative_area_level_1",
          "political"
        ]
      },
      {
        "long_name": "Ireland",
        "short_name": "IE",
        "types": [
          "country",
          "political"
        ]
      }
    ],
    "formatted_address": "Dublin 8, Ireland",
    "geometry": {
      "bounds": {
        "northeast": {
          "lat": 53.37132399999999,
          "lng": -6.2642699
        },
        "southwest": {
          "lat": 53.325953,
          "lng": -6.3563488
        }
      },
      "location": {
        "lat": 53.34785249999999,
        "lng": -6.318115199999999
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "northeast": {
          "lat": 53.37132399999999,
          "lng": -6.2642699
        },
        "southwest": {
          "lat": 53.325953,
          "lng": -6.3563488
        }
      }
    },
    "place_id": "ChIJLz14IT8MZ0gR2uKxYpAO0Bc",
    "types": [
      "postal_town"
    ]
  },
  {
    "address_components": [
      {
        "long_name": "Dublin City",
        "short_name": "Dublin City",
        "types": [
          "administrative_area_level_2",
          "political"
        ]
      },
      {
        "long_name": "County Dublin",
        "short_name": "County Dublin",
        "types": [
          "administrative_area_level_1",
          "political"
        ]
      },
      {
        "long_name": "Ireland",
        "short_name": "IE",
        "types": [
          "country",
          "political"
        ]
      }
    ],
    "formatted_address": "Dublin City, Co. Dublin, Ireland",
    "geometry": {
      "bounds": {
        "northeast": {
          "lat": 53.4111566,
          "lng": -6.11309
        },
        "southwest": {
          "lat": 53.2988569,
          "lng": -6.387080600000001
        }
      },
      "location": {
        "lat": 53.3603142,
        "lng": -6.315054200000001
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "northeast": {
          "lat": 53.4111566,
          "lng": -6.11309
        },
        "southwest": {
          "lat": 53.2988569,
          "lng": -6.387080600000001
        }
      }
    },
    "place_id": "ChIJv2RI7foRZ0gRwAKA8azHAAM",
    "types": [
      "administrative_area_level_2",
      "political"
    ]
  },
  {
    "address_components": [
      {
        "long_name": "Dublin",
        "short_name": "Dublin",
        "types": [
          "locality",
          "political"
        ]
      },
      {
        "long_name": "County Dublin",
        "short_name": "County Dublin",
        "types": [
          "administrative_area_level_1",
          "political"
        ]
      },
      {
        "long_name": "Ireland",
        "short_name": "IE",
        "types": [
          "country",
          "political"
        ]
      }
    ],
    "formatted_address": "Dublin, Ireland",
    "geometry": {
      "bounds": {
        "northeast": {
          "lat": 53.42521010000001,
          "lng": -6.0439235
        },
        "southwest": {
          "lat": 53.22343009999999,
          "lng": -6.4474847
        }
      },
      "location": {
        "lat": 53.3498053,
        "lng": -6.2603097
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "northeast": {
          "lat": 53.42521010000001,
          "lng": -6.0439235
        },
        "southwest": {
          "lat": 53.22343009999999,
          "lng": -6.4474847
        }
      }
    },
    "place_id": "ChIJL6wn6oAOZ0gRoHExl6nHAAo",
    "types": [
      "locality",
      "political"
    ]
  },
  {
    "address_components": [
      {
        "long_name": "County Dublin",
        "short_name": "County Dublin",
        "types": [
          "administrative_area_level_1",
          "political"
        ]
      },
      {
        "long_name": "Ireland",
        "short_name": "IE",
        "types": [
          "country",
          "political"
        ]
      }
    ],
    "formatted_address": "Co. Dublin, Ireland",
    "geometry": {
      "bounds": {
        "northeast": {
          "lat": 53.6347257,
          "lng": -5.9962748
        },
        "southwest": {
          "lat": 53.1781971,
          "lng": -6.5468798
        }
      },
      "location": {
        "lat": 53.3302033,
        "lng": -6.3105655
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "northeast": {
          "lat": 53.6347257,
          "lng": -5.9962748
        },
        "southwest": {
          "lat": 53.1781971,
          "lng": -6.5468798
        }
      }
    },
    "place_id": "ChIJv2RI7foRZ0gRwAKA8azHABg",
    "types": [
      "administrative_area_level_1",
      "political"
    ]
  },
  {
    "address_components": [
      {
        "long_name": "Ireland",
        "short_name": "IE",
        "types": [
          "country",
          "political"
        ]
      }
    ],
    "formatted_address": "Ireland",
    "geometry": {
      "bounds": {
        "northeast": {
          "lat": 55.4602,
          "lng": -5.668900100000001
        },
        "southwest": {
          "lat": 51.3066,
          "lng": -10.7666001
        }
      },
      "location": {
        "lat": 53.41291,
        "lng": -8.24389
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "northeast": {
          "lat": 55.4602,
          "lng": -5.668900100000001
        },
        "southwest": {
          "lat": 51.3066,
          "lng": -10.7666001
        }
      }
    },
    "place_id": "ChIJ-ydAXOS6WUgRCPTbzjQSfM8",
    "types": [
      "country",
      "political"
    ]
  }
]
halfer
  • 19,824
  • 17
  • 99
  • 186
Onyx
  • 5,186
  • 8
  • 39
  • 86
  • Post the proper object, not copied from console.log – Code Maniac Mar 17 '19 at 18:34
  • What do you mean by that, where else can I copy it from? – Onyx Mar 17 '19 at 18:36
  • 1
    You can do something like this and post a valid JS data [copy from console](https://stackoverflow.com/questions/10305365/javascript-chrome-how-to-copy-an-object-from-the-webkit-inspector-as-code) – Code Maniac Mar 17 '19 at 18:38
  • So either 'locality' or 'political' in the types array, should be a match right? – Jibin Joseph Mar 17 '19 at 18:40
  • @godof23 I'm trying to get a match if both are in the types array. Though I wouldn't mind having a code that matches if either are in the types array either since that might come in handy later. – Onyx Mar 17 '19 at 18:44
  • @Bobimaru you can use `find` along with `every` for both to match and `some` for atleast one, – Code Maniac Mar 17 '19 at 18:47

2 Answers2

2

You could find the element that includes every of the mentioned types:

 array.find(it => ['locality', 'political'].every(type => it.types.includes(type)))
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • Out of curiosity, is it possible to alter this code so that locality and political are mandatory whilst having an option to add a third optional element to the array like 'country"? – Onyx Mar 17 '19 at 19:13
  • @bobimaru yes it is :) – Jonas Wilms Mar 17 '19 at 19:15
  • Is it lengthy change? I tried changing every() to some(), however, this way the code matches the first element which has either of the 3 values but I need to match at least 2. – Onyx Mar 17 '19 at 19:19
  • Something else to consider: what happens when multiple objects satisfy your conditions (or does it even matter?) – mattsven Mar 17 '19 at 19:20
  • @bobimaru you mean 2 out of 3? Thats another question btw – Jonas Wilms Mar 17 '19 at 19:22
1

So, let's examine what's going on here.

Problem: you have a array of objects, and you need to find and return the one that has the correct types array.

Well, first, we'll need to somehow examine each of the objects in the array — a for loop will work well for this. Specifically, we can use the for(... of ...) pattern:

for(let object of array){
    /* do stuff */
}

Next, we'll need to test each of these objects based on the conditions we've set. We know we'll need to examine each object's types property, so we'll start there:

for(let object of array){
    if(someKindOfTest(object.types)){
        return object;
    }
}

So how do we actually perform this test? Assuming we know what types we're looking for...

let requiredTypes = ["locality"];

for(let object of array){
    if(someKindOfTest(object.types)){
        return object;
    }
}

...we can devise a test that only passes if the object has every one of the required types. Sounds like a job for another loop:

let requiredTypes = ["locality"];

for(let object of array){
    let hasAllRequiredTypes = true;

    //  test for each of the required types

    for(let requiredType of requiredTypes){
        if(!object.types.includes(requiredType)){
            // this object doesn't match one of our required types, that disqualifies it

            hasAllTypes = false; // flag this object as disqualified
            break; // end this loop over the required types
        }
    }

    if(hasAllRequiredTypes) return object;
}

All of this can be compacted into a shorter, more "functional" version (@jonas-wilms answer is a great example of this), but I thought it useful to outline the underlying mechanisms and problem solving that would go into devising an algorithm like this.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
mattsven
  • 22,305
  • 11
  • 68
  • 104