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"
]
}