简体   繁体   中英

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.

Each of the x values will have to have AT LEAST 4 more subsequent values (likely a bit more). 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.

TotNumPlayers 

will iterate through a given set declared at the beginning (somewhat like 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]. 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.

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 is an array of players and each player is represented by an object with some properties, (name, data, etc). 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)

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]

to add to it, just push into the sub array activeRange[0].otherData.push(newValue)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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