JavaScript Storing Data in 2-D Array

2019-07-25 16:04发布

问题:

I would like to check elements in Data array and display as "O" if it contains that Month and Number in a table. So for "Jan-1", Array1[0][0] should be displayed as "O" but code below doesn't work. Can someone help me?

var Data = ["Jan-1", "Feb-4", "Apr-5"];
var Month= ["Jan", "Feb", "Mar", "Apr", "May"];
var Number = ["1", "2", "3", "4", "5"];
var Array1 = [[]];

for (var k = 0; k < Data.length; k++) {
    var split = Data[k].split("-");
    for (var z = 0; z < Month.length; z++) {
        for (var s = 0; s < Number.length; s++) {
            if (Month[z] == split[0] && period[s] == split[1]) {
                Array1[z][s] = "O";
            } else {
                Array1[z][s] = "X";
            }             
        }
    }
}
number/month |  Jan  |  Feb  |  Mar  |  Apr  | May
------------------------------------------------------
     1       |   O   |   X   |   X   |   X   |   X  
     2       |   X   |   X   |   X   |   X   |   X  
     3       |   X   |   X   |   X   |   X   |   X  
     4       |   X   |   O   |   X   |   X   |   X  
     5       |   X   |   X   |   X   |   O   |   X  

回答1:

You should loop over Number and Month array, and each time check if the combination Month[j] + "-" + Number[i] is in the Data array:

var Data = ["Jan-1", "Feb-4", "Apr-5"];

var Month = ["Jan", "Feb", "Mar", "Apr", "May"];
var Number = ["1", "2", "3", "4", "5"];

var result = [];


for (var i = 0; i < Number.length; i++) {                   // foreach number
  result[i] = [];                                           // create a row for this current number
  for (var j = 0; j <Month.length; j++) {                   // for each month
    if (Data.indexOf(Month[j] + "-" + Number[i]) !== -1) {  // check if the current combination (currentMonth-currentNumber) is in the Data array
      result[i][j] = "O";
    } else {
      result[i][j] = "X";
    }
  }
}

result.forEach(function(row) {
  console.log(row.join(" | "));
});



回答2:

Analyzing your code:

var Data = ["Jan-1", "Feb-4", "Apr-5"];
var Month= ["Jan", "Feb", "Mar", "Apr", "May"];
var Number = ["1", "2", "3", "4", "5"];
var Array1 = [  ["X","X","X","X","X"],  ["X","X","X","X","X"],  "X","X","X","X","X"],  ["X","X","X","X","X"],  ["X","X","X","X","X"]]; //Initialise all elements to "X" by default. We shall change only those indexes that match.

for (var k = 0; k < Data.length; k++) {
    var split = Data[k].split("-");
    for (var z = 0; z < Month.length; z++) {
        for (var s = 0; s < Number.length; s++) {
            if (Month[z] == split[0] && Number[s] == split[1]) {
                Array1[z][s] = "O";
            } else {
                //Array1[z][s] = "X"; Do not change here as the loop will go over the entire array once for each data. Hence previous matches would get lost!!
            }             
        }
    }
}

Finally, console.table(Array1); to print values.

Comment: While initialising, you are declaring rows as months and columns as days but expect opposite output. So, to generate expected output, print transverse form or change while defining values:

if (Month[z] == split[0] && Number[s] == split[1]) {
            Array1[s][z] = "O";
        }


回答3:

You could take an object with the given data and iterate month and number for returning a new array with the information of 'O' or 'X'.

var data = ["Jan-1", "Feb-4", "Apr-5"],
    month = ["Jan", "Feb", "Mar", "Apr", "May"],
    number = ["1", "2", "3", "4", "5"],
    result = [],
    hash = Object.create(null);

data.forEach(function(s) {
    var [m, d] = s.split('-');
    hash[m] = hash[m] || {};
    hash[m][d] = true;
});

result = month.map(function (m, z) {
    return number.map(function (s) {
        return (hash[m] || {})[s] ? 'O' : 'X';
    });
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }