Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

71
Visualizações
Variable.map is not a function React Javascript

I am working on app and I am trying to extract three attributes of this string (I thought it was an array of objects is filteredResultsJSON, but it is a string) :

enter image description here

Well, when I want to get the news of a given section I am using this code:

export default function getNewsFilteredBySection (sectionSearched="") {

    const URL= `https://api.nytimes.com/svc/mostpopular/v2/viewed/7.json?api-key=${API_KEY}`;
   
    
  return fetch(URL)
        .then(res=>res.json())
        .then(response=> {
            const { data = [] } = response;
            if(Array.isArray(data)){
                const {results} = response; 
                const filteredResults = results.filter(obj => {
                    return obj.section === sectionSearched;
                  });
                const filteredResultsJSON = JSON.stringify(filteredResults);
                const name = typeof(filteredResultsJSON);
                console.log(name);
                return filteredResultsJSON;
            }
        })


};

I did JSON.stringify(filteredResults) because I saw it when doing console.log()as an [Object Object] and I wanted to be able to read the information and to loop through it to be able to extract it.

So I am using this code to extract the attributes I want for each news:

export function getNewsAttributes (filteredResultsJSON=[]) {
        const newsAttributes = filteredResultsJSON.map(filteredResultJSON=>{
            const { title, abstract, url } = filteredResultJSON;
            console.log(title, abstract, url);
            return {title, abstract, url}; 
          });
        return newsAttributes;
};

But it returns the following error:

enter image description here

I have been researching and I totally think the matter is about JSON.Stringify(filteredResults). How could I maintain that variable as an object, being able to read it and extract the information from it?

I am going to use it like this, after an onClick event:

function showEvent (e) {
      const sectionSearched = e.target.innerText;
      const filteredResultsJSON = getNewsFilteredBySection(sectionSearched);
      const newsAttributes = getNewsAttributes(filteredResultsJSON);

    }

Thanks a lot :)

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I solved it with this code:

export default function getNewsFilteredBySection (sectionSearched="") {

    const URL= `https://api.nytimes.com/svc/mostpopular/v2/viewed/7.json?api-key=${API_KEY}`;
   
    
  return fetch(URL)
        .then(res=>res.json())
        .then(response=> {
            const { data = [] } = response;
            if(Array.isArray(data)){
                const {results} = response; 
                const filteredResults = results.filter(obj => {
                    return obj.section === sectionSearched;
                  });
                console.log(filteredResults);
                return filteredResults;
            }
        })


};

Finally I didnt need to do JSON.Stringify(filteredResults). However I am having this two errors and I dont know why, my app works:

enter image description here

Any idea? Thanks a lot :)

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to add async

export default async function getNewsFilteredBySection (sectionSearched=""){ ...


showEvent = async (e) => { ...

and await

const filteredResultsJSON = await getNewsFilteredBySection(sectionSearched);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda