Add values into 2D dimensional array in JS

I’m trying to add values into 2D array based on the position that is present in the input data.

For example, the below format represents 0 as row, 0 as column and 5 is length of the value.

 [
    "0,0,5",
    "hello"
 ],

How do I insert the values into 2D array based on position like [0][0], [0][1] and [0][2]?

Except from my code

const data = [
  [
    "0,0,5",
    "hello"
  ],
  [
    "0,1,10",
    "js is fun!"
  ],
  [
    "0,2,0",
    ""
  ]
]

let array2D = [
  []
];
let i = 0
for (let r = 0; r < data.length; ++r) {
  array2D[r] = [];
  for (let c = 0; c < data.length; ++c) {
    array2D[r][c] = data[i++];
  }
}
console.log(array2D);

1 thought on “Add values into 2D dimensional array in JS”

  1. Ok, thank you for your input.. it means that I’ll just make a function to place into an array with 4 arguments.. col,row,arr&data

    //important function
    function place2d(row,col,arr,data){
      //row col logic works like arr[row][col]
      arr[row]=arr[row]||[]
      arr[row][col]=data
    }
    
    var array2dArray=[]
    //this loop would take the data array and place the entire index in the desired destination
    data.forEach(a=>{
      var [row,col]=a[0].split(',')
      place2d(row,col,array2dArray,a) //the data to put into each part of the 2d array would an array itself(like data[0])
    })
    console.log(array2dArray)
    
    //but I might just wanna put the text in the 2d array
    var text2dArray=[]
    data.forEach(a=>{
      var [row,col]=a[0].split(',')
      place2d(row,col,text2dArray,a[1]) //the data to be put in each part of the 2d array would be the a text variable(like data[0][1] is "hello")
    })
    console.log(text2dArray)
    <script>
    //sry it just takes space in the js part that's unnessecary
    window.data = [
      [
        "0,0,5",
        "hello"
      ],
      [
        "0,1,10",
        "js is fun!"
      ],
      [
        "0,2,0",
        ""
      ]
    ]
    </script>

    With this function, you can take an empty array, place row 10 col 4 in empty array putting any data like ‘xD’ and it will work out eg: try place2d(10,4,emptyArrName,"xD") and it works.. just one thing to note..

    IT ONLY APPLIES array structures to WHERE IT NEEDS TO.. doing things like the example above would leave a lot of undefined slots.. wild example below

    //now for a wild example to show that the function works
    window.data2=[
      ["text for [10][0]","0/10"],
      ["text for [2][5]","5/2"]
    ]
    //don't forget the placer function :D
    function place2d(row,col,arr,data){
      //row col logic works like arr[row][col]
      arr[row]=arr[row]||[]
      arr[row][col]=data
    }
    //at the end of the day, all I'm changing is the information I make out of the array in the forEach loops in order to simply place in the function
    var finalArray=[]
    place2d(3,4,finalArray,"randomDataThatCouldBe_ANYTHING_notJustText")
    data2.forEach(a=>{
      var [col,row]=a[1].split('/')
      place2d(row,col,finalArray,a[0])
    })
    console.log(finalArray)
    Reply

Leave a Comment