Show unique values of an array only once in divs

you can use JS vanilla and loop with forEach on each div with grid class and giving the innerText property the corresponding keyword after the shuffle.

var keywords = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"];

(function() {
  shuffle(keywords);
  
  const grids = document.querySelectorAll('.grid');
  
  grids.forEach(function(grid, i) {
    grid.innerText = keywords[i];
  });
})();

function shuffle(array) {
  var m = array.length,
    t, i;
  while (m > 0) {
    i = Math.floor(Math.random() * m--);
    t = array[m];
    array[m] = array[i];
    array[i] = t;
  }
  return array;
}
<div class="grid-wrapper">
  <div class="grid g1" data-row="0" data-column="0">
    <h1>Show value 1 of array</h1>
  </div>
  <div class="grid g2" data-row="0" data-column="1">
    <h1>Show value 2 of array</h1>
  </div>
  <div class="grid g3" data-row="0" data-column="2">...</div>
  <div class="grid g4" data-row="0" data-column="3">...</div>
  <div class="grid g5" data-row="0" data-column="4">...</div>
  <div class="grid g6" data-row="4" data-column="0">...</div>
  <div class="grid g7" data-row="4" data-column="1">...</div>
  <div class="grid g8" data-row="4" data-column="2">...</div>
  <div class="grid g9" data-row="4" data-column="3">...</div>
  <div class="grid g10" data-row="4" data-column="3">...</div>
</div>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top