Javascript multidimensional array updating specific element

后端 未结 5 1485
一向
一向 2021-01-05 19:48

I have a string that has been converted into an 2D Array in js.

board = "...|.X.|...|"

It is used to represent a game board

each . represent

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 20:27

    The situation and solution given above is pretty simple. The issue of updating specific values in a list of objects (often referred to as an array, but that's a discussion for a different time) has more practical and industrial application. The problem you tend to run into is thinking that looking at a value in a specific cell, e.g. my_array[0][0] returns 'some value' will also let you change that value through an assignment e.g. my_array[0][0] = 'new value'. You will find that depending on how you defined your array, the change shows in the same row across the columns, not what you are needing. Look at the example code as an illustration of creating, and managing a multidimensional list of objects (array).

    
    
    JavaScript Object List/Array
    
    
    
      

    look in the JavaScript console for output

提交回复
热议问题