JavaScript-Tutorial: So erstellen Sie eine Liste mithilfe einer Matrix
P粉617237727
P粉617237727 2023-09-06 09:54:44
0
2
573
<p><strong>我有一个像这样的数组</strong></p> <pre class="brush:php;toolbar:false;">const input_array= [ ["schwarz", "blau"], ["groß", "mittel"], ["a", "b", "c"] //... ist es dynamisch, es können viele Zeilen hinzugefügt werden ];</pre> <p><strong>我该如何得到一个像这样的数组:</strong></p> <pre class="brush:php;toolbar:false;">const finallist = [ ["schwarz", "groß", "a"], ["schwarz", "groß", "b"], ["schwarz", "groß", "c"], ["schwarz", "mittel", "a"], ["schwarz", "mittel", "b"], ["schwarz", "mittel", "c"], ["blau", "groß", "a"], ["blau", "groß", "b"], ["blau", "groß", "c"], ["blau", "mittel", "a"], ["blau", "mittel", "b"], ["blau", "mittel", "c"], ]</pre> <p><strong>请记住input_array是动态的</strong></p> <p><strong>请告诉我如何做到这一点</strong></p>
P粉617237727
P粉617237727

Antworte allen(2)
P粉729518806

你可以像这样做:

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

看一下这个,可能会有帮助:

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 

小心! 在大规模矩阵中可能会导致浏览器崩溃。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage