.filter doesn't make much sense here, because you don't need to create an intermediate array, you just need to find a value. You might use .find to return a administrative_area_level_2, if found, else an administrative_area_level_1, else unknown:
export const getDistrict = ({ address_components }) => {
const lvl2 = address_components.find(({ types }) => types.includes('administrative_area_level_2'));
if (lvl2) return lvl2.long_name;
const lvl1 = address_components.find(({ types }) => types.includes('administrative_area_level_1'));
return lvl1 ? lvl1.long_name : 'Unknown';
};
Or, to keep your code more DRY, abstract the .find and long_name access into a separate function:
const makeFindType = arr => levelNum => {
const found = arr.find(({ types }) => types.includes('administrative_area_level_' + levelNum));
if (found) return found.long_name;
};
export const getDistrict = ({ address_components }) => {
const findType = makeFindType(address_components);
return findType('2') || findType('1') || 'Unknown';
};