Tutoriel JavaScript : Comment créer une liste à l'aide d'une matrice
P粉617237727
P粉617237727 2023-09-06 09:54:44
0
2
514
<p><strong>我有一个像这样的数组</strong></p> <pre class="brush:php;toolbar:false;">const input_array= [ ["noir", "bleu"], ["grand", "moyen"], ["a", "b", "c"] //... est-ce dynamique, peut-on ajouter plusieurs lignes ];≪/pré> <p><strong>我该如何得到一个像这样的数组:</strong></p> <pre class="brush:php;toolbar:false;">const finallist = [ ["noir", "grand", "a"], ["noir", "grand", "b"], ["noir", "grand", "c"], ["noir", "moyen", "a"], ["noir", "moyen", "b"], ["noir", "moyen", "c"], ["bleu", "grand", "a"], ["bleu", "grand", "b"], ["bleu", "grand", "c"], ["bleu", "moyen", "a"], ["bleu", "moyen", "b"], ["bleu", "moyen", "c"], ]≪/pré> <p><strong>请记住input_array是动态的</strong></p> <p><strong>请告诉我如何做到这一点</strong></p>
P粉617237727
P粉617237727

répondre à tous(2)
P粉729518806

Vous pouvez faire quelque chose comme ceci :

const input_array = [
    ["black", "blue"],
    ["large", "medium"],
    ["a", "b", "c"]
]
const getCompinations = array =>
  array.reduce((v, b) =>
    v.reduce((r, g) => [...r, ...b.map(w => [].concat(g, w))], [])
  )
console.log(getCompinations(input_array))
P粉449281068

Jetez un œil à ceci, cela pourrait aider :

const input_array = [
    ["black", "blue"],
    ["large", "medium"],
    ["a", "b", "c"]
    //... 是否可以动态添加多行
];
const mmc = input_array.reduce((e, r) => e * r.length, 1);
const finallist = input_array.map((x,i)=>({index:i,arr:x})).reduce((e, r) => {
    for (var u = 0; u  e[u].includes(r)) || e[u].length 

Soyez prudent ! Peut provoquer un crash du navigateur dans une grande matrice.

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!