To get a single element through the.filter array

Asked

Viewed 37 times

0

Using array.map and array.filter, I select only one object from address, but his return contains a array empty-indexed.

The 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 the expected would be:

 {
  "long_name": "long name",
  "short_name": "short name",
  "types": [
    "administrative_area_level_1"
  ]
}
  • Marcelo, in the question you give example of only 1 object, but can have more right? and can have more than one for each address_components?

  • Yes, @Sergio. I finished reducing in the example used.

  • Okay, so my answer should do what you want. If you wanted to cut back you could use .find also.

  • Yes, your answer has already directed me to a positive outcome.

  • Okay, but that’s what we gave in the answers you were looking for or still missing something?

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);

2

I suggest filtering first because you reduce the array size for further processing, and then map what you pass the filter.

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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.