简体   繁体   English

如何在JavaScript中推入二维数组的第二维?

[英]How to push into the second dimension of a two dimensional array in JavaScript?

I have a list of players in denoted as 我有一个球员名单,表示为

activeRange[x]

where x will vary from day-to-day. 其中x每天都会变化。

Each of the x values will have to have AT LEAST 4 more subsequent values (likely a bit more). x值中的每一个都必须至少有4个后续值(可能更多)。 Ideally I'd like the array to look like: 理想情况下,我希望数组看起来像:

activeRange[x][y]

So here's what I've done so far: 所以这是我到目前为止所做的:

var MATCH = AllData[TotalRows][TotalColumns+1];
activeRange[TotNumPlayers].push(MATCH);

This is all located within 3 nested for loops. 全部位于3个嵌套的for循环内。

TotNumPlayers 

will iterate through a given set declared at the beginning (somewhat like 23). 将迭代开头声明的给定集合(有点像23)。 Once done, the 完成后,

TotalRows 

will iterate, then finally 会迭代,然后最终

TotalColumns

I'm running into the following error: 我遇到以下错误:

TypeError: Cannot find function push in object mitch

mitch is the value of activeRange[0]. mitch是activeRange [0]的值。 I've been staring at this way too long, so any help would be appreciated! 我一直盯着这种方式太久了,因此我们将不胜感激!

EDIT: Code inserted below: PLEASE IGNORE ALL THE COMMENTS. 编辑:在下面插入代码:请忽略所有评论。 I COPY/PASTED THIS FROM A BIT OF CODE I USED YESTERDAY TO PERFORM A DIFFERENT FUNCTION. 我复制/粘贴了昨天使用的部分代码以执行不同的功能。 This is the second time I've ever posted on this website, so trying to format this monster to be pretty was scary sounding. 这是我第二次在该网站上发布内容,因此尝试将这个怪物格式化为漂亮的声音听起来很吓人。 Hopefully this is good enough. 希望这足够好。

This is how activeRange was declared and initialized. 这就是activeRange的声明和初始化方式。

var activeRange = new Array();
for (var b=0; b<=lastRow-2; b++){
    activeRange[b] = sheetRANK.getRange(b+2,1).getValue();
}

This is the function. 这就是功能。

function getTotalScore(activeRange, w) {
    Logger.clear()
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheetWAR = ss.getSheetByName('WAR');
    var sheetRANK = ss.getSheetByName('RANK');

    var AllData = sheetRANK.getDataRange().getValues();
    Logger.log('First');
    for (var TotNumPlayers = 0; TotNumPlayers <= activeRange.length; TotNumPlayers++) {
        Logger.log('Second');
        var f = 0;
        for (var TotalColumns = 0; TotalColumns <= AllData[0].length; ++TotalColumns) { // Init n. If n <= the total columns (second dimension), inc n.
            Logger.log('Third');
            for (var TotalRows = 0; TotalRows <= AllData.length; ++TotalRows) { // Init i. If i <= the total rows (first dimension), inc i.
                Logger.log('Fourth');
                //try{ // to avoid errors.
                if (activeRange[TotNumPlayers] != "") {
                    Logger.log('Here?');
                    if (AllData[TotalRows][TotalColumns].valueOf().toUpperCase() == activeRange[TotNumPlayers].toUpperCase()) {
                        Logger.log('How About Here?');
                        var MATCH = AllData[TotalRows][TotalColumns + 1];
                        activeRange.push(TotNumPlayers, MATCH);
                        for (var Calc = 0; Calc <= activeRange[TotNumPlayers].length - 1; Calc++) {
                            var OverallScore = ((activeRange[TotNumPlayers][0] * 1.0) + (activeRange[TotNumPlayers][1] * .75) + (activeRange[TotNumPlayers][2] * .50) + (activeRange[TotNumPlayers][3] * .25));
                            sheetRANK.getRange(activeRange[TotNumPlayers] + 1, 2).setValue(OverallScore);

                            f = f + 1;
                        }
                        if (TotalRows == AllData.length - 1 && TotalColumns == AllData[0].length - 1 && f == 0) {
                            Browser.msgBox('No names matching \'' + activeRange[TotNumPlayers] + '\' found. Check your spelling!');
                            return;
                        }
                    }
                }
            }
        }
    }
}

Try thinking about what kind of data structures you can use to make your life easier. 尝试考虑可以使用哪种数据结构来简化生活。 For this particular case, you have a list of players that you want to associate some data with. 对于这种特殊情况,您具有要与之关联数据的玩家列表。 You'd probably use a structure like: 您可能会使用类似以下的结构:

activeRange = [
  {
    name: 'mitch',
    data: []
  }
]

When you want to update the data, you'd simply call activeRange[0].data.push(someData) . 当您想更新数据时,只需调用activeRange[0].data.push(someData)

activeRange is an array of players and each player is represented by an object with some properties, (name, data, etc). activeRange是一组播放器,每个播放器由具有某些属性(名称,数据等)的对象表示。 Calling activeRange[0] yields the first player in your array and activeRange[0].data will yield the data associated with that player, which you can then manipulate however you want ( push , pop , etc) 调用activeRange[0]产生数组中的第一个播放器,而activeRange[0].data将产生与该播放器关联的数据,然后您可以根据需要进行操作( pushpop等)

Based on your comments, you need a structure more like this 根据您的评论,您需要一个更像这样的结构

var activeRange = [
    {
      name: 'mitch',
      otherData: [
        10,
        11,
        12, 
        13
      ]
    },
    {
      name: 'viper',
      otherData: [
        //values
      ]
    }
]

you can access that by activeRange[0].otherData[2] 您可以通过activeRange[0].otherData[2]

to add to it, just push into the sub array activeRange[0].otherData.push(newValue) 要添加它,只需将其推入子数组activeRange[0].otherData.push(newValue)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM