0

I'm struggling with something somehow basic :)

As you can see in this line of code, I use an index (i) to select a certain value inside an array:

tiles[i].addChild(block);

and I would like to be able to do the same, but to push something into an array, on this line:

tile0Content.push(block.identifier);

I don't know how to do this, but I imagine something like this (with i instead of 0):

tileiContent.push(block.identifier);

I could of course manually copy paste my code but it's very unpractical + my index will vary depending on situations so it would be very VERY practical to be able to push values inside my arrays using an index :)

thx to everyone reading my post <3

EDIT: I think my post isnt perfectly clear on what I want to accomplish, but I could sum it up like this: Instead of doing:

tile0Content.push(block.identifier);
tile1Content.push(block.identifier);
tile2Content.push(block.identifier);

I would like to do:

for(var i = 0;i<myIndex.length;i++){
tileiContent.push(block.identifier);
}
  • 3
    Seems like you want to apply the same idea that you did with `tiles` but to `tilesContent`, so that `tileContent` is an array of inner arrays, so that you can perform `tileContent[i].push(blick.identifier)` – Nick Parsons Apr 14 '22 at 13:29
  • Yes exactly! Is that possible to do? – Lightweight Digital Apr 14 '22 at 13:37
  • Yes, it is, rather than creating seperate variables such as `title1Content`, `tile2Content` etc..., you can create an array called `tileContent` and within that array, each element at each index would represent your associated variables at the moment. So if `title1Content = [1, 2, 3]` and `title2Content = [4, 5, 6]` at the moment, you would create an array of the form `titleContent = [[1, 2, 3], [4, 5, 6], ...]`. – Nick Parsons Apr 14 '22 at 13:41
  • 1
    Thanks a lot, that totally solve my problem! – Lightweight Digital Apr 14 '22 at 13:49

0 Answers0