Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

69
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda