简体   繁体   中英

Pass Multidimensional Array Elements to Div in Js

I have looked and haven't found any solutions to my issue. I have a grid like a puzzle.

  <div id="grid-container">
      <div class="item" id="item1"><a href="#"></a></div>
      <div class="item" id="item2"><a href="#"></a></div>
      <div class="item" id="item3"><a href="#"></a></div>
      <div class="item" id="item4"><a href="#"></a></div>
      <div class="item" id="item5"><a href="#"></a></div>
      <div class="item" id="item6"><a href="#"></a></div>
      <div class="item" id="item7"><a href="#"></a></div>
      <div class="item" id="item8"><a href="#"></a></div>
      <div class="item" id="item9"><a href="#"></a></div>
      <div class="item" id="item10"><a href="#"></a></div>
      <div class="item" id="item11"><a href="#"></a></div>
      <div class="item" id="item12"><a href="#"></a></div>
      <div class="item" id="item13"><a href="#"></a></div>
      <div class="item" id="item14"><a href="#"></a></div>
      <div class="item" id="item15"><a href="#"></a></div>
      <div class="item" id="item16"><a href="#"></a></div>
      <div class="item" id="item17"><a href="#"></a></div>
      <div class="item" id="item18"><a href="#"></a></div>
      <div class="item" id="item19"><a href="#"></a></div>
      <div class="item" id="item20"><a href="#"></a></div>
      <div class="item" id="item21"><a href="#"></a></div>
      <div class="item" id="item22"><a href="#"></a></div>
      <div class="item" id="item23"><a href="#"></a></div>
      <div class="item" id="item24"><a href="#"></a></div>
      <div class="item" id="item25"><a href="#"></a></div>
      <div class="item" id="item26"><a href="#"></a></div>
      <div class="item" id="item27"><a href="#"></a></div>
      <div class="item" id="item28"><a href="#"></a></div>
      <div class="item" id="item29"><a href="#"></a></div>
      <div class="item" id="item30"><a href="#"></a></div>
      <div class="item" id="item31"><a href="#"></a></div>
      <div class="item" id="item32"><a href="#"></a></div>
      <div class="item" id="item33"><a href="#"></a></div>
      <div class="item" id="item34"><a href="#"></a></div>
      <div class="item" id="item35"><a href="#"></a></div>
      <div class="item" id="item36"><a href="#"></a></div>
    </div>

and I have an array of what I want to fill the puzzle with:

var ws = [
    ['A', 'P', 'P', 'L', 'E', 'P'],
    ['A', 'G', 'C', 'A', 'A', 'I'],
    ['A', 'R', 'A', 'A', 'A', 'Z'],
    ['A', 'A', 'A', 'A', 'T', 'Z'],
    ['P', 'P', 'A', 'A', 'A', 'A'],
    ['A', 'E', 'P', 'R', 'A', 'Y']
];

I have also created class to print out the array.

class Puzzle {

  constructor(width, height) {
    this.width = (width)? width : 6;
    this.height = (height)? height : 6;

    if(this.width != this.height)
      throw "height and width must be the same"
  }
  print(){
    console.log(ws)
  }
}

puz = new Puzzle(6, 6);

puz.print();

What I want to do is to pass the output of the array into each div with IDs: item1..36.

I have used

document.getElementById('myArray').innerHTML = ws;

to try to get it to work but its not working. Please I need a better approach to it.

You may try this inside your print function. (my guess is your print will update the UI)

 print() { for(var i =1 ; i<= this.height; i++){ for(var j = 1; j <= this.width; j++) { document.getElementById(`item${(i-1) * this.height + j}`).innerHTML = ws[i-1][j-1]; } } }

Hope this will help!

you need to create a matrix on the contructor,

class Puzzle {

  constructor(width, height) {
   var ws = [
    ['A', 'P', 'P', 'L', 'E', 'P'],
    ['A', 'G', 'C', 'A', 'A', 'I'],
    ['A', 'R', 'A', 'A', 'A', 'Z'],
    ['A', 'A', 'A', 'A', 'T', 'Z'],
    ['P', 'P', 'A', 'A', 'A', 'A'],
    ['A', 'E', 'P', 'R', 'A', 'Y']
];

    if(this.width != this.height)
      throw "height and width must be the same"
  }
  print(){
    console.log(this)
  }
}

puz = new Puzzle(6, 6);

now you can create a function that update the page and put values on elements: you put all items on an 1D array and then iterate items with 2 for cycle:

function updatePage(var x){

    var elements = document.getElementsByClassName("item");
    var k = 0;

    for(var i = 0; i < puzzle.height; i++){
        for(var j = 0; j < puzzle.width; j++){
            elements[k].innerHTML = x.grid[i][j];
            k++;
        }
    }
}

and finally you can call your function to view grid elements on the page:

updatePage(puzz);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM