2

I'm writing this code to find the long-name of "types" : [ "locality", "political" ] and long-name of "types" : [ "administrative_area_level_2", "political" ] from the json file. but unable to iterate through the array properly

js file

var request = require('request');
request('http://maps.googleapis.com/maps/api/geocode/json?latlng=23.6519148,87.13857650000001', function(error, response, data) {
    if (!(!error && response.statusCode == 200)) {
        console.log('error! ');
        return;
    }

    data = JSON.parse(data);
    for (var i in data.results) {
        for (var j in data.results[i]) {
            if (j == 'address_components') {
                console.log('found')
                console.log(data.results[i][j][1]);
                console.log(data.results[i][j][2]);
                for (var k in data.results[i]){
                    if(k == 'long_name')
                        console.log('found')
                }
                console.log('not found')
            }
        }
        break;
    }
});

output

found
{ long_name: 'Kunustoria',
  short_name: 'Kunustoria',
  types: [ 'locality', 'political' ] }
{ long_name: 'Bardhaman',
  short_name: 'Bardhaman',
  types: [ 'administrative_area_level_2', 'political' ] }
not found
3
  • What do you need as output? Commented Apr 22, 2016 at 5:08
  • long_name of "types" : [ "locality", "political" ] and long_name of "types" : [ "administrative_area_level_2", "political" ]..... that is "Kunustoria" and "Bardhaman" Commented Apr 22, 2016 at 5:11
  • Point of clarity. Once you execute this line data = JSON.parse(data); you are no longer dealing with JSON, but rather a regular JavaScript object. Commented Apr 22, 2016 at 5:12

2 Answers 2

1

Replace your for loop with this...

data = JSON.parse(data).results;

data.forEach(function (address) {
  console.log(address['address_components'][0].long_name);
});
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, this gives the first long name of all the address_components, but I want the 2nd and 3rd long name of 1st address_components. I will try to get that using your loop :)
0

First you must install lodash.

npm install --save lodash

var _ = require('lodash');
var data = api_response_from_google_maps;//Assign the api response here.
var addrs = [];
_.map(data.results, function (address) {
  _.map(address.address_components, function (item) {
    if (_.isEqual(item.types, ["locality", "political"]) || _.isEqual(item.types, ["administrative_area_level_1", "political"])) {
      addrs.push(item);
    }
  })
});

console.log(JSON.stringify(addrs));

This gives an array of objects of adresses_components with types array as you specified.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.