How to get a single element through array.filter

0

Using array.map and array.filter , I select only one object of address , but its return contains array with empty indexes.

array has the following structure:

[
  {
    "formatted_address": "275-291 Bedford Ave, Brooklyn, NY 11211, USA",
    "address_components": [
      {
        "long_name": "long name",
        "short_name": "short name",
        "types": [
          "route"
        ]
      }
    ]
  },
  {
    "formatted_address": "Williamsburg, NY, USA",
    "address_components": [
      {
        "long_name": "long name",
        "short_name": "short name",
        "types": [
          "locality"
        ]
      }
    ]
  },
  {
    "formatted_address": "New York 11211, USA",
    "address_components": [
      {
        "long_name": "long name",
        "short_name": "short name",
        "types": [
          "postalCode"
        ]
      }
    ]
  },
  {
    "formatted_address": "Kings, New York, USA",
    "address_components": [
      {
        "long_name": "long name",
        "short_name": "short name",
        "types": [
          "locality"
        ]
      }
    ]
  },
  {
    "formatted_address": "Brooklyn, New York, USA",
    "address_components": [
      {
        "long_name": "long name",
        "short_name": "short name",
        "types": [
          "administrative_area_level_2"
        ]
      }
    ]
  },
  {
    "formatted_address": "New York, New York, USA",
    "address_components": [
      {
        "long_name": "long name",
        "short_name": "short name",
        "types": [
          "administrative_area_level_2"
        ]
      }
    ]
  },
  {
    "formatted_address": "New York, USA",
    "address_components": [
      {
        "long_name": "long name",
        "short_name": "short name",
        "types": [
          "administrative_area_level_1"
        ]
      }
    ]
  },
  {
    "formatted_address": "United States",
    "address_components": [
      {
        "long_name": "long name",
        "short_name": "short name",
        "types": [
          "country"
        ]
      }
    ]
  }
]

let address = [];

address.push({
  formatted_address: "275-291 Bedford Ave, Brooklyn, NY 11211, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['route']
  }]
});
address.push({
  formatted_address: "Williamsburg, NY, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['locality']
  }]
});
address.push({
  formatted_address: "New York 11211, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['postalCode']
  }]
});
address.push({
  formatted_address: "Kings, New York, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['locality']
  }]
});
address.push({
  formatted_address: "Brooklyn, New York, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['administrative_area_level_2']
  }]
});
address.push({
  formatted_address: "New York, New York, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['administrative_area_level_2']
  }]
});
address.push({
  formatted_address: "New York, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['administrative_area_level_1']
  }]
});
address.push({
  formatted_address: "United States",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['country']
  }]
});


let administrative_area_level_1 = address.map((address) => {
  return address.address_components.filter((elem, i, array) => {
    if (elem.types.includes('administrative_area_level_1')) {
      return elem;
    }
  });
});

console.log(administrative_area_level_1);

After treatment, it is returned:

[
  [],
  [],
  [],
  [],
  [],
  [],
  [
    {
      "long_name": "long name",
      "short_name": "short name",
      "types": [
        "administrative_area_level_1"
      ]
    }
  ],
  []
]

When expected would be:

 {
  "long_name": "long name",
  "short_name": "short name",
  "types": [
    "administrative_area_level_1"
  ]
}
    
asked by anonymous 23.08.2017 / 17:05

2 answers

2
let address = [];

address.push({
  formatted_address: "275-291 Bedford Ave, Brooklyn, NY 11211, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['route']
  }]
});
address.push({
  formatted_address: "Williamsburg, NY, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['locality']
  }]
});
address.push({
  formatted_address: "New York 11211, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['postalCode']
  }]
});
address.push({
  formatted_address: "Kings, New York, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['locality']
  }]
});
address.push({
  formatted_address: "Brooklyn, New York, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['administrative_area_level_2']
  }]
});
address.push({
  formatted_address: "New York, New York, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['administrative_area_level_2']
  }]
});
address.push({
  formatted_address: "New York, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['administrative_area_level_1']
  }]
});
address.push({
  formatted_address: "United States",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['country']
  }]
});


address.map((address) => {
  return address.address_components.filter((elem, i, array) => {
    if (elem.types.includes('administrative_area_level_1')) {
       administrative_area_level_1 = elem;
    }
  });
});

console.log(administrative_area_level_1);
    
23.08.2017 / 17:16
2

I suggest filtering first because you soon reduce the size of the array for future processing, and then map what the filter passes.

let address = [];

address.push({
  formatted_address: "275-291 Bedford Ave, Brooklyn, NY 11211, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['route']
  }]
});
address.push({
  formatted_address: "Williamsburg, NY, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['locality']
  }]
});
address.push({
  formatted_address: "New York 11211, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['postalCode']
  }]
});
address.push({
  formatted_address: "Kings, New York, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['locality']
  }]
});
address.push({
  formatted_address: "Brooklyn, New York, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['administrative_area_level_2']
  }]
});
address.push({
  formatted_address: "New York, New York, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['administrative_area_level_2']
  }]
});
address.push({
  formatted_address: "New York, USA",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['administrative_area_level_1']
  }]
});
address.push({
  formatted_address: "United States",
  address_components: [{
    long_name: 'long name',
    short_name: 'short name',
    types: ['country']
  }]
});


let administrative_area_level_1 = address.filter((address) => {
  return address.address_components.find(
    comp => comp.types.includes('administrative_area_level_1')
  );
}).map(address => address.address_components);

console.log(administrative_area_level_1);
    
23.08.2017 / 17:36