Generating a 3D Array from a 2D Array

I am trying to create a music game where I need to create a 3D array from a basic 2D array. The plan was to copy and paste the 2D array 4 times into the 3D array before changing it, as shown below:

var note3base = [
["C", "E", "G"],
["C#", "E#", "G#"],
["Db", "F", "Ab"],
["D", "F#", "A"],
["Eb", "G", "Bb"],
["E", "G#", "B"],
["F", "A", "C"],
["F#", "A#", "C#"],
["Gb", "Bb", "Db"],
["G", "B", "D"],
["Ab", "C", "Eb"],
["A", "C#", "E"],
["Bb", "D", "F"],
["B", "D#", "F#"],
["Cb", "Eb", "Gb"]
];

var note3 = new Array(4);

for (h=0;h<note3.length;h++){
note3[h] = note3base;
} //creates 4 copies of note3base in a 3d-array to be modified

for (i=0;i<note3[0].length;i++){
note3[1][i][1] = flat(note3[1][i][1]); //minor
note3[2][i][1] = flat(note3[2][i][1]);
note3[2][i][2] = flat(note3[2][i][2]); //dim
note3[3][i][2] = sharp(note3[3][i][2]); //aug
} //how did everything become the same?

Now the problem is that the loop forseems to apply the method to each individual array (from 0 to 3).

The desired output for note 3 [0] [1] would be CEG, note 3 [1] [1] would be C Eb G, note [2] [1] would be C Eb Gb, note [3] [1] would be CEG #.

Any help is much appreciated!

I have included (working) sharp and flat methods below for reference:

function sharp(note){
  var newnote;
  if (note.charAt(1) == "#"){
    newnote = note.replace("#", "x");
  } else if (note.charAt(1) == "b"){
    newnote = note.replace("b", "");
  } else {
    newnote = note + "#";
  }
  return newnote;
 }

function flat(note){
   var newnote;
   if (note.charAt(1) == "#"){
     newnote = note.replace("#", "");
   } else {
     newnote = note + "b";
   }
   return newnote;
}
+4
source share
2 answers

, , , :

someVar = someArray;

... , . ( , - .) , , :

for (h=0;h<note3.length;h++){
  note3[h] = note3base;

... note3 .

, , .slice() :

for (h=0;h<note3.length;h++){
  note3[h] = note3base.slice();
}

, note3base , .slice() . , note3[0] note3[1] ( 2 3) , note3[0][0] note3[1][0] note3[2][0] note3[3][0] ["C", "E", "G"]. ( .)

, " ". :

for (h=0;h<note3.length;h++){
  // create this element as a new empty array:
  note3[h] = [];
  // for each 3-note array in note3base
  for (var k = 0; k < note3base.length; k++) {
    // make a copy with slice
    note3[h][k] = note3base[k].slice();
  }
}

, , , note3base, , , :

function makeNote3Array() {
  return [
    ["C", "E", "G"],
    ["C#", "E#", "G#"],
    ["Db", "F", "Ab"],
    ["D", "F#", "A"],
    ["Eb", "G", "Bb"],
    ["E", "G#", "B"],
    ["F", "A", "C"],
    ["F#", "A#", "C#"],
    ["Gb", "Bb", "Db"],
    ["G", "B", "D"],
    ["Ab", "C", "Eb"],
    ["A", "C#", "E"],
    ["Bb", "D", "F"],
    ["B", "D#", "F#"],
    ["Cb", "Eb", "Gb"]
  ];
}

, , . , , .slice() :

var note3 = new Array(4);
for (h=0;h<note3.length;h++){
  note3[h] = makeNote3Array();
}
+3

TL; DR, :

for (h=0;h<note3.length;h++){
  note3[h] = note3base.slice(0);
}

:

- " " " " Javascript.

, a = "string";, , b = a;, b 'by-value': b, b . "" , a b.

a = "string";
b = a;
a = "gnirts";
console.log(b);   // "string" 

, - , . b ' ', , [1, 2, 3], a b . , a, b, . , :

a = [1, 2, 3];
b = a;
a[0] = "hello";
console.log(b);   // ["hello", 2, 3]

b[0] , , a[0]. , note3base , . note3base.slice(0), .

:

+3

All Articles