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

216
Views
ejecutar la función de mapa en un resultado directamente desde una API da error

Estoy teniendo un tipo de problema extraño. Tengo una función getPosts que devuelve un objeto json, cuando ejecuto una función .map() en él, aparece el error TypeError: getPosts(...).forEach is not a function , pero cuando almaceno el valor de getPosts en una variable, como json1 y luego ejecute la función .map() en ella, funciona perfectamente. No sé por qué está pasando eso.

 async function getPosts() { let posts = await fetch('https://jsonplaceholder.typicode.com/posts') .then(response => response.json()) .then(json => { return json.slice(0, 5) }) } var json2=["Ike","Ron"]; var i=0; getPosts().forEach(element => { element.first_name=json2[i]; i++; }); console.log(getPosts())

Registro de errores

TypeError: getPosts(...).forEach is not a function

el resultado de getPosts almacenado en var json1 es

 let json1 = [ { userId: 1, id: 1, title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', body: 'quia et suscipit\n' + 'suscipit recusandae consequuntur expedita et cum\n' + 'reprehenderit molestiae ut ut quas totam\n' + 'nostrum rerum est autem sunt rem eveniet architecto' }, { userId: 1, id: 2, title: 'qui est esse', body: 'est rerum tempore vitae\n' + 'sequi sint nihil reprehenderit dolor beatae ea dolores neque\n' + 'fugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\n' + 'qui aperiam non debitis possimus qui neque nisi nulla' } ]

cuando ejecuto la función .map() aquí, funciona bien y obtengo el resultado esperado

 var json2=["Ike","Ron"]; var i=0; json1.forEach(element => { element.first_name=json2[i]; i++; }); console.log(json1)

el resultado esperado es el siguiente y funciona bien

 [ { userId: 1, id: 1, title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', body: 'quia et suscipit\n' + 'suscipit recusandae consequuntur expedita et cum\n' + 'reprehenderit molestiae ut ut quas totam\n' + 'nostrum rerum est autem sunt rem eveniet architecto', first_name: 'Ike' }, { userId: 1, id: 2, title: 'qui est esse', body: 'est rerum tempore vitae\n' + 'sequi sint nihil reprehenderit dolor beatae ea dolores neque\n' + 'fugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\n' + 'qui aperiam non debitis possimus qui neque nisi nulla', first_name: 'Ron' } ]

código actualizado después de devolver las publicaciones

 async function getPosts() { let posts = await fetch('https://jsonplaceholder.typicode.com/posts') .then(response => response.json()) .then(json => json.slice(0, 5)) return posts } getPosts()
about 4 years ago · Juan Pablo Isaza
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!