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

200
Views
.map is not a function (api result)

I'm trying to map an api result to display the data but when I launch my code the console shows TypeError: pokemons.map is not a function but I do the same in another script and it's working there. I don't understand why this time .map doesn't work.

const result = document.getElementById('result');
const form = document.querySelector('form');
let pokemons = [];

async function fetchPokemon() {
  fetch(`https://pokeapi.co/api/v2/pokemon/ditto`)
    .then((res) => res.json())
    .then((data) => (pokemons = data));

  console.log(pokemons)
};

fetchPokemon();

function pokemonDisplay() {

  result.innerHTML = pokemons.map(
    (pokemon) =>
    `
    <h2>${pokemon.forms[0].name}</h2>
    `
  )
}

form.addEventListener('submit', (e) => {
  e.preventDefault();
  fetchPokemon().then(() => pokemonDisplay());
})
console.log(result);
<div class="app">
  <form action="">
    <label for="search">Entrez le nom d'un pokemon (en anglais)</label>
    <input type="text" id="search" placeholder="ex : ditto">
  </form>

  <ul id="result"></ul>
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

https://pokeapi.co/api/v2/pokemon/ditto return object not array (Ditto pokemon). map is the function of Array, that is why you catch this error.

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!