Ich habe derzeit die folgende Matrix

const fileName = [
  ["a01", "b01", "c01", "d01", "e01", "f01", "g01", "h01", "i01", "j01", "k01", "l01"],
  ["a02", "b02", "c02", "d02", "e02", "f02", "g02", "h02", "i02", "j02", "k02", "l02"],
  ["a03", "b03", "c03", "d03", "e03", "f03", "g03", "h03", "i03", "j03", "k03", "l03"],
  ["a04", "b04", "c04", "d04", "e04", "f04", "g04", "h04", "i04", "j04", "k04", "l04"],
  ["a05", "b05", "c05", "d05", "e05", "f05", "g05", "h05", "i05", "j05", "k05", "l05"],
  ["a06", "b06", "c06", "d06", "e06", "f06", "g06", "h06", "i06", "j06", "k06", "l06"],
  ["a07", "b07", "c07", "d07", "e07", "f07", "g07", "h07", "i07", "j07", "k07", "l07"],
  ["a08", "b08", "c08", "d08", "e08", "f08", "g08", "h08", "i08", "j08", "k08", "l08"],
  ["a09", "b09", "c09", "d09", "e09", "f09", "g09", "h09", "i09", "j09", "k09", "l09"],
  ["a10", "b10", "c10", "d10", "e10", "f10", "g10", "h10", "i10", "j10", "k10", "l10"],
  ["a11", "b11", "c11", "d11", "e11", "f11", "g11", "h11", "i11", "j11", "k11", "l11"],
  ["a12", "b12", "c12", "d12", "e12", "f12", "g12", "h12", "i12", "j12", "k12", "l12"]
];

Und ich erstelle ein neues Array mit Dateinamen daraus, wobei 1 Element jedes Subarrays mit dieser Funktion zufällig ausgewählt wird:

function randomise() {
  let sequence = fileName.map(option => {
    const random = Math.floor(Math.random() * 11);
    return option[random];
  });
  let randomSelection = sequence.map(createURL);
  function createURL(fileName) {
    return `assets/music/${fileName}.mp3`;
  }
  console.log(randomSelection);
}

Also bekomme ich ein Array wie:

["assets/music/f01.mp3", "assets/music/f02.mp3", "assets/music/b03.mp3", "assets/music/k04.mp3", "assets/music/b05.mp3", "assets/music/f06.mp3", "assets/music/i07.mp3", "assets/music/d08.mp3", "assets/music/d09.mp3", "assets/music/g10.mp3", "assets/music/a11.mp3", "assets/music/d12.mp3"]

Aber ich möchte meine Matrix folgendermaßen neu anordnen:

const fileName = [
  ["a01", "a02", "a03", "a04", "a05", "a06", "a07", "a08", "a09", "a10", "a11", "a12"],
  ["b01", "b02", "b03", "b04", "b05", "b06", "b07", "b08", "b09", "b10", "b11", "b12"],
  ["c01", "c02", "c03", "c04", "c05", "c06", "c07", "c08", "c09", "c10", "c11", "c12"],
  ["d01", "d02", "d03", "d04", "d05", "d06", "d07", "d08", "d09", "d10", "d11", "d12"],
  ["e01", "e02", "e03", "e04", "e05", "e06", "e07", "e08", "e09", "e10", "e11", "e12"],
  ["f01", "f02", "f03", "f04", "f05", "f06", "f07", "f08", "f09", "f10", "f11", "f12"],
  ["g01", "g02", "g03", "g04", "g05", "g06", "g07", "g08", "g09", "g10", "g11", "g12"],
  ["h01", "h02", "h03", "h04", "h05", "h06", "h07", "h08", "h09", "h10", "h11", "h12"],
  ["i01", "i02", "i03", "i04", "i05", "i06", "i07", "i08", "i09", "i10", "i11", "i12"],
  ["j01", "j02", "j03", "j04", "j05", "j06", "j07", "j08", "j09", "j10", "j11", "j12"],
  ["k01", "k02", "k03", "k04", "k05", "k06", "k07", "k08", "k09", "k10", "k11", "k12"]
];

Ich muss zufällig 1 Element aus jedem der Indizes dieser Subarrays auswählen, sodass ein zufälliges Element mit "1" endet, ein anderes mit "2" usw. Könnten Sie mir bitte helfen? Vielen Dank!

3
Guillermo Brachetta 19 Feb. 2020 im 16:19

3 Antworten

Beste Antwort

Wenn dies die tatsächlichen Werte des von Ihnen verwendeten Arrays sind, können Sie einfach eine Schleife von 1 bis 12 durchführen und diese mit einem zufälligen Zeichen aus der Zeichenfolge "abcdefghijk" unter Verwendung von randojs 'rando() -Funktion (oder sonst wenn Sie es vorziehen).

for(var i = 1; i <= 12; i++){
  console.log("assets/music/" + rando("abcdefghijk") + (i < 10 ? "0" : "") + i + ".mp3")
}
<script src="https://randojs.com/1.0.0.js"></script>

Dieser Code verwendet randojs.com, um die Zufälligkeit zu vereinfachen und das Lesen zu vereinfachen. Wenn Sie diesen Code also verwenden möchten, Stellen Sie sicher, dass sich dies im Head-Tag Ihres HTML-Dokuments befindet:

<script src="https://randojs.com/1.0.0.js"></script>

Um den zweiten Teil Ihrer Frage zu beantworten (den Sie als weitere Antwort auf diese Frage gepostet haben), müssen Sie Ihre Variable fileName nicht beibehalten, um den HTML-Code hier zu erstellen lieber nicht. Sie können es stattdessen so machen:

var letters = "abcdefghijk";
for(var i = 0; i < letters.length; i++){
    var musicRowID = letters.charAt(i) + "01";
    $("#music-grid").append(`<div id="music-row-${musicRowID}" class="row no-gutters"></div>`);

    for(var j = 1; j <= 12; j++){
        var columnID = letters.charAt(i) + (j < 10 ? "0" : "") + j;
        $(`#music-row-${musicRowID}`).append(`<div class="col-1"><button id="${columnID}" class="btn bar song">${columnID.toUpperCase()}</button></div>`);
    }
}
1
Aaron Plocharczyk 21 Feb. 2020 im 02:13

Wenn ich Sie richtig verstanden habe, erhalten Sie die gewünschte Ausgabe. Wählen Sie einen Buchstaben für jede Nummer. Hoffe das hilft für alles was du brauchst.

const fileName = [
  ["a01", "a02", "a03", "a04", "a05", "a06", "a07", "a08", "a09", "a10", "a11", "a12"],
  ["b01", "b02", "b03", "b04", "b05", "b06", "b07", "b08", "b09", "b10", "b11", "b12"],
  ["c01", "c02", "c03", "c04", "c05", "c06", "c07", "c08", "c09", "c10", "c11", "c12"],
  ["d01", "d02", "d03", "d04", "d05", "d06", "d07", "d08", "d09", "d10", "d11", "d12"],
  ["e01", "e02", "e03", "e04", "e05", "e06", "e07", "e08", "e09", "e10", "e11", "e12"],
  ["f01", "f02", "f03", "f04", "f05", "f06", "f07", "f08", "f09", "f10", "f11", "f12"],
  ["g01", "g02", "g03", "g04", "g05", "g06", "g07", "g08", "g09", "g10", "g11", "g12"],
  ["h01", "h02", "h03", "h04", "h05", "h06", "h07", "h08", "h09", "h10", "h11", "h12"],
  ["i01", "i02", "i03", "i04", "i05", "i06", "i07", "i08", "i09", "i10", "i11", "i12"],
  ["j01", "j02", "j03", "j04", "j05", "j06", "j07", "j08", "j09", "j10", "j11", "j12"],
  ["k01", "k02", "k03", "k04", "k05", "k06", "k07", "k08", "k09", "k10", "k11", "k12"]
];

let pickedValues = [];

for (i = 0; i <= fileName.length; i++) {
  let index = Math.floor(Math.random() * ((fileName.length - 1) - 0 + 1));
  pickedValues[i] = (fileName[index][i]);
}
console.log(pickedValues);
0
Aaron 19 Feb. 2020 im 15:54

Sehr schöner Aaron! Ich habe das Array folgendermaßen erstellt:

let randomSelection = new Array();

function randomise() {
  for (let i = 1; i <= 12; i++) {
    let index = `assets/music/${rando("abcdefghijkl")}${i < 10 ? "0" : ""}${i}.mp3`;
    randomSelection.push(index);
  }
}

randomise()

Das einzige Problem ist jetzt, dass ich den folgenden Code verwendet habe, um ein Raster basierend auf meiner fileName Variablen zu füllen ...

fileName.forEach(row => {
  $("#music-grid").append(`<div id="music-row-${row.slice(0, 1)}" class="row no-gutters"></div>`);
  row.forEach(col => {
    $(`#music-row-${row.slice(0, 1)}`).append(
      `<div class="col-1"><button id="${col}" class="btn bar song">${col.toUpperCase()}</button></div>`
    );
  });
});

Halten Sie es für besser, meine ursprüngliche fileName -Variable beizubehalten, damit sie das Raster füllen kann?

Vielen Dank!

1
Guillermo Brachetta 21 Feb. 2020 im 01:20