Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

70
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!