-1
I have a map function that needs to confirm if certain values exist so that certain gaps can be filled.
The function that tests if the values exist was created but when returning the value inside the map it returns as Undefined, the console.log() inside the function assures me that it is working but in the map it is going as Undefined.
Response.data.items comes from a google database where I am using map to retrieve certain data, as not all data has the item.pagemap.localbusiness, I created an if that checks if it exists before trying to use the map on it. Follow the map code:
placesInfo = [...response.data.items.map((item) => {
const isExist = (obj, src) => {
console.log (obj.map(item => item[src])) //Esse console.log me garante que a função está funcionando
return obj.map(item => item[src])
}
if(!item.pagemap.localbusiness) {
return {
title: item.title,
link: item.link,
snippet: item.snippet,
name: '',
email: '',
telephone: '',
address: ''
}
}
else return {
title: item.title,
link: item.link,
snippet: item.snippet,
name: isExist(item.pagemap.localbusiness, 'name') ? item.name: null, //Aqui o valor sempre vem como undefined
email: isExist(item.pagemap.localbusiness, 'email') ? item.email: null, //Aqui o valor sempre vem como undefined
telephone: isExist(item.pagemap.localbusiness, 'telephone') ? item.telephone: null, //Aqui o valor sempre vem como undefined
address: isExist(item.pagemap.localbusiness, 'address') ? item.address: null, //Aqui o valor sempre vem como undefined
}})]
At the end of the map, I have an array with results like this:
{
title: 'Cláudio Souza Barbearia - Barbearia em Muriqui',
link: 'https://claudio-souza-barbearia.negocio.site/',
snippet: 'Cláudio Souza Barbearia. Barbearia em Muriqui. Abre amanhã às 09:00. \n' +
'Solicitar cotaçãoLigar agoraVer rotasWhatsAppEnvie-nos uma mensagemEntrar \n' +
name: undefined,
email: undefined,
telephone: undefined,
address: undefined
}
However, the.log console upstairs assures me that there are results to be filled in where we have Undefined, the.log console returns things to me like:
[
'Wagner Salão de Beleza Masculino',
'Wagner Salão de Beleza Masculino',
'DILMA ESTÉTICA.COM',
'BARBEARIA COPACABANA - COPACABANA',
'SALAO E BARBEARIA INTERNACIONAL',
'BARBEARIA NOSSA SENHORA DA PENHA',
'BARBEARIA ATLÂNTICA LTDA',
'BARBEARIA BLUE STAR',
'BARBEARIA TRÊS AMIGOS LTDA',
'AUGUSTO GUEDES ALVES',
'VICENTE PINHEIRO DOS PASSOS',
'BARBEARIA',
'CABELEIREIRO KAIOS',
'SALAO ELITE BARBEARIA LTDA',
'IRMÃOS FERNANDES BARBEARIA LTDA',
'SALÃO NOSSA SENHORA PAZ BARBEARIA',
'SALÃO E BARBEARIA PAPISO'
]
[ 'Barbearia Rodrigues' ]
Any possible solution?
Please elaborate a [mcve] to demonstrate the problem cited. In the code snippet you put in the question, many objects have not been defined and the functions do not exist, which makes it impossible to reproduce the cited error.
– Woss
I updated the post, see if it’s a little clearer, please
– Arthur Lopes
What struck me is that in that expression
isExist(item.pagemap.localbusiness, 'name') ? item.name: null
will always returnitem.name
, forisExist()
the result is always an array, even if empty, and the operator?
will induce the result ofisExist()
a boolean. Well in javascript an independent array of being or not empty is always converted to booleantrue
: exampleconsole.log([])
. Which implies thatitem.name
,item.email
,item.telephone
anditem.address
are not defined.– Augusto Vasques