Search code examples
javascriptjquerygame-physics

Change numbers in object with many arrays


I have a fields object with multiple one dimensional arrays written in JavaScript. I want to be able to change numbers in the object, by clicking a button or whatever. One way is, that i create a complete new object, with just that change. But i don't want to create every time a complete new one.

So how is it possible to change the 11 in the middle of the row to any other number? I need this change for my tilemap.

var fields = [
[2,2,2,2,2,7,7,7,7,7,7,7,7,7,7,7,0],
[7,1,2,2,7,7,7,7,7,7,7,7,7,3,5,7,0],
[7,7,7,7,7,7,7,1,7,7,7,7,7,7,7,7,0],
[7,7,7,7,7,7,7,7,7,7,7,7,7,7,0,0,0],
[7,7,3,7,7,7,7,7,0,7,7,7,4,7,0,0,0],
[0,0,0,0,0,7,7,7,0,0,7,7,7,7,7,0,0],
[2,2,2,0,0,7,7,7,0,0,0,7,7,2,2,2,2],
[2,1,2,2,0,7,7,7,7,7,0,2,2,2,2,0,0],
[7,7,2,2,0,0,7,7,11,7,0,0,0,0,0,0,0], // this row i want change the 11
[7,7,7,2,0,7,7,7,7,7,7,7,2,2,0,0,0],
[7,7,7,7,7,7,7,7,7,7,7,7,7,2,2,2,2],
[0,0,7,7,7,7,7,7,7,7,7,7,7,2,2,2,2],
[0,0,0,7,7,0,7,7,7,7,3,7,7,2,2,2,2],
[2,7,0,0,0,0,7,0,7,7,7,7,7,1,2,2,2],
[2,1,7,7,7,0,0,0,7,7,7,7,2,2,2,0,0],
[2,2,2,2,7,0,7,7,7,7,7,2,2,0,0,0,0],
[2,2,2,2,2,2,7,7,7,7,7,2,0,0,0,0,0], 

]

Solution

  • var fields = [
    [2,2,2,2,2,7,7,7,7,7,7,7,7,7,7,7,0],
    [7,1,2,2,7,7,7,7,7,7,7,7,7,3,5,7,0],
    [7,7,7,7,7,7,7,1,7,7,7,7,7,7,7,7,0],
    [7,7,7,7,7,7,7,7,7,7,7,7,7,7,0,0,0],
    [7,7,3,7,7,7,7,7,0,7,7,7,4,7,0,0,0],
    [0,0,0,0,0,7,7,7,0,0,7,7,7,7,7,0,0],
    [2,2,2,0,0,7,7,7,0,0,0,7,7,2,2,2,2],
    [2,1,2,2,0,7,7,7,7,7,0,2,2,2,2,0,0],
    [7,7,2,2,0,0,7,7,11,7,0,0,0,0,0,0,0], // this row i want change the 11
    [7,7,7,2,0,7,7,7,7,7,7,7,2,2,0,0,0],
    [7,7,7,7,7,7,7,7,7,7,7,7,7,2,2,2,2],
    [0,0,7,7,7,7,7,7,7,7,7,7,7,2,2,2,2],
    [0,0,0,7,7,0,7,7,7,7,3,7,7,2,2,2,2],
    [2,7,0,0,0,0,7,0,7,7,7,7,7,1,2,2,2],
    [2,1,7,7,7,0,0,0,7,7,7,7,2,2,2,0,0],
    [2,2,2,2,7,0,7,7,7,7,7,2,2,0,0,0,0],
    [2,2,2,2,2,2,7,7,7,7,7,2,0,0,0,0,0], 
    ];
    
    function changethe11(){
      fields[8][8] = 32;
      console.log(fields);
    }
    <button onclick=changethe11()>change the 11</button>