creating 2 dimension arrays

后端 未结 1 541
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-21 05:47

I am new to JS and need some basic help:

I have a spreadsheet which has a square matrix of data.

I can read these data as follows:

  var freq         


        
相关标签:
1条回答
  • 2020-12-21 06:24

    Someone much smarter than myself could probably help you write this better, but the way it looks to me is that you want to create tempArr as the main array, which would have 2 arrays inside of it.

    Inside of each of those arrays are values, so:

    // result: tempArr = [[1,2],[4,5]]
    tempArr = [] // or new Array
    for (var i = 0; i <= 3; i++) {
      tempArr[i] = [];
      for (var j = 0; j <= 3; j++) {        
        tempArr[i].push(freqArr[i][j]);
      }
    }
    

    You create your first main array in tempArr, within your for, each time you loop through, tempArr[i] is created as an array, and inside of the second for, you want to push the value of freqArr to the inner array.

    UPDATE Had a space in tempArr[i] that would've caused it to not work for sure. Sorry!

    0 讨论(0)
提交回复
热议问题