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
?– Sergio
Yes, @Sergio. I finished reducing in the example used.
– Marcelo de Andrade
Okay, so my answer should do what you want. If you wanted to cut back you could use
.find
also.– Sergio
Yes, your answer has already directed me to a positive outcome.
– Marcelo de Andrade
Okay, but that’s what we gave in the answers you were looking for or still missing something?
– Sergio