How do I call an array within an IIFE using a forEach loop?

Jonathlon

I have an array that's inside an IIFE, then I have a forEach loop that iterates over those array items but I don't know how to then call the function using the forEach loop.

//IIFE - Immediately Invoked Function Expression
let pokemonRepository = (function () {

  //List of Pokemon Characters
  let pokemonList = [
    { name: "Pikachu", height: 1.04, type: 'electric' },
    { name: "Bulbasaur", height: 2.04, type: ['grass', 'poison'] },
    { name: "Squirtle", height: 1.08, type: 'water' },
    { name: "Beedrill", height: 3.03, type: ['bug', 'poison'] },
    { name: "Dragonite", height: 7.03, type: ['dragon', 'flying'] },
    { name: "Igglybuff", height: 1.01, type: ['Normal', 'Fairy'] },
  ]

  function add(pokemon) {
    pokemonList.push(pokemon);
  }

  function getAll() {
    return pokemonList;
  }

  return {
    add: add,
    getAll: getAll
  };
})();

// Test of return functions inside IIFE 
// console.log(pokemonRepository.getAll());
// pokemonRepository.add({ name: 'Sandstorm' });
// console.log(pokemonRepository.getAll()); // [ { name: 'Sandstorm' } ]


// forEach loop
pokemonList.forEach(function (pokemon) {
  if (pokemon.height >= 7) {
    document.write("<div class='card'>" + "<p>" + pokemon.name + " " + "(Height:" + " " + pokemon.height + ") - Wow! that is a big pokemon! " + "</p>" + "</div>");
  } else if (pokemon.height) {
    document.write("<div class='card'>" + "<p>" + pokemon.name + " " + "(Height:" + " " + pokemon.height + ")  " + "</p>" + "</div>")
  }
});

I can call the items in the console.log using;

console.log(pokemonRepository.getAll());

but I want to call the repository using the forEach loop in the DOM.

Mureinik

You answered yourself - getAll returns the array, so you need to call it:

pokemonRepository.getAll().forEach(function (pokemon) {
  if (pokemon.height >= 7) {
    document.write("<div class='card'>" + "<p>" + pokemon.name + " " + "(Height:" + " " + pokemon.height + ") - Wow! that is a big pokemon! " + "</p>" + "</div>");
  } else if (pokemon.height) {
    document.write("<div class='card'>" + "<p>" + pokemon.name + " " + "(Height:" + " " + pokemon.height + ")  " + "</p>" + "</div>")
  }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I call a function within an IIFE expression

How do I structure an array within an .each loop using jQuery?

How do I loop through using foreach for a json decoded array

How do i loop through these objects inside an array using forEach?

How do I create and name checkboxes within a foreach loop PHP

How do I print foreach loop content within a controller?

How do I use foreach within a while loop

Array within array using foreach loop in codeigniter

How to call a function within a foreach parallel loop

How do I update an api call within a while loop?

How do I "loop" through JSON array inside foreach loop?

How do I loop through this array with a foreach loop?

How do I position IIFE charts within an HTML page?

How do I get an index out of an array, that was created using a foreach loop, and lives inside a text file?

How do I append a string to an array of strings within a loop in C?

How do I call a function within ajax using codeigniter to update

How do I insert data into an array in a foreach loop?

How do I return array data from a foreach loop in Angular

How do I exit an Array.forEach loop early?

How to make a nested function call within an IIFE?

How can I call a function within the foreach loop and avoid redeclaring error

how can I restructure an associative array using a PHP foreach loop (or not)?

Call Function within ForEach Loop

I require just a single array output within the foreach loop. How can I accomplish

How do I loop through array and call function for each?

How do I update an object within an array using findIndex?

How do I declare an a range within an array using an enumeration in Ada?

How do I convert strings in an array to numbers using forEach?

How Do I Pass on an Array of Objects using "forEach" Function?