Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8277317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:39:39+00:00 2026-06-08T08:39:39+00:00

I am trying to choose an element in an array based on one of

  • 0

I am trying to choose an element in an array based on one of it’s children’s values.

Using the results returned from the Google Maps API geocode request as an example, we have an array of the form:

{
      "address_components": [
        {
          "long_name": "Renshaw St",
          "short_name": "Renshaw St",
          "types": [
            "route"
          ]
        },
        {
          "long_name": "Eccles",
          "short_name": "Eccles",
          "types": [
            "locality",
            "political"
          ]
        },
        {
          "long_name": "Greater Manchester",
          "short_name": "Gt Man",
          "types": [
            "administrative_area_level_2",
            "political"
          ]
        },
        {
          "long_name": "United Kingdom",
          "short_name": "GB",
          "types": [
            "country",
            "political"
          ]
        },
        {
          "long_name": "M30 0PQ",
          "short_name": "M30 0PQ",
          "types": [
            "postal_code"
          ]
        },
        {
          "long_name": "Manchester",
          "short_name": "Manchester",
          "types": [
            "postal_town"
          ]
        }
      ],
      "formatted_address": "Renshaw St, Eccles, Manchester, Greater Manchester M30 0PQ, UK",
      "geometry": {
        "bounds": {
          "northeast": {
            "lat": 53.48266090000001,
            "lng": -2.35206530
          },
          "southwest": {
            "lat": 53.48128999999999,
            "lng": -2.35324410
          }
        },
        "location": {
          "lat": 53.4822450,
          "lng": -2.35294730
        },
        "location_type": "GEOMETRIC_CENTER",
        "viewport": {
          "northeast": {
            "lat": 53.48332443029150,
            "lng": -2.351305719708498
          },
          "southwest": {
            "lat": 53.48062646970850,
            "lng": -2.354003680291502
          }
        }
      },
      "types": [
        "route"
      ]
    }

I’d like to choose the address_component->long_name that has the element type equal to a value I specify.

I have written the following code that takes the above array and a key to find as parameters and seems to work well:

function getAddressComponent(key, result) {

  var rtn;

  $.each(result,function(index,value){
    if( result[index].types[0] == key ) rtn = result[index].long_name;
  });

  return rtn;    
}

But was wondering if anyone could suggest a better approach as I have seen it done a few different ways and would like to use the optimal one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:39:41+00:00Added an answer on June 8, 2026 at 8:39 am

    Your code isn’t that long.. lol. You can return the result immediately because once you found it, you can leave the loop. And I didn’t investige the jQuery forEach but it couldn’t be shorter than the code below, I reckon.

    How is this?

    function findAdressComponentByLongName( findString ) {
    
        var addressComponents = data.address_components,
            i = 0,
            ln = addressComponents.length;
    
        for( ; i < ln; i++ ) {
    
            if( addressComponents[ i ].long_name == findString ) {
    
                return addressComponents[ i ];
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to choose a certificate from the computer certificate store LocalMachine\My that serves
I am trying to validate html select element using jQuery Validate plugin. I set
I am trying to unbind the mouseup event from an element. I have tried
I'm trying to add uploaded files from a directory to a form element in
I'm trying to choose some files to upload in Windows Phone 7 (in the
Having problems with a small awk script, Im trying to choose the newest of
I'm trying to decide how to choose between the WSO2 Gadget Server and WSO2
I am trying to set up Emacs so that when I choose Compile.. in
I'm trying to send ethernet packet to choosed destination MAC address using jPcap :
I am trying to return records based on the cities that someone chooses. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.