2

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);

5
  • so you would want "0,0,5" and place the 5 char text in 2dArray[0][0]? Commented Feb 15, 2021 at 19:54
  • @TheBombSquad Correct. The 2dArray[0][0] should have the value hello Commented Feb 15, 2021 at 19:55
  • one more thing.. can i assume data would always be in that format? example.. normalArr[2] pointing to 2dArr[0][2]? or is it just each array pointing to any 2d place? Commented Feb 15, 2021 at 19:59
  • The second one. It just each array pointing to any 2d place. There could be [2][0] or [1][2] and other positions as well. @TheBombSquad Commented Feb 15, 2021 at 20:01
  • ok.. check it out now :D Commented Feb 15, 2021 at 20:19

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

4 Comments

Wow! This is great!
oh.. i was in the middle of showing one last example(a wild one).. it shows that it only places structures where it has to place(and leaves the rest of data alone).. therefore if you place in broad areas with nothing in between.. there will be lots of undefined parts but the parts that you wanted placed would be normal.. can i show that or leave it as is? @scriobh
Sure, that would help. It would be just avoiding undefined. Correct?
correct @scriobh it avoids everything else besides where you told it to go(row and col) so if i tell it to go to row 15 and column 12 in an emptyArr, emptyArr[0] straight to emptyArr[14] would be undefined AND emptyArr[15][0] to emptyArr[15][11] would be undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.