[英]In JavaScript, I can't make unique values of arrays of strings, and exclude determinated values from another array
只有两个问题:
let titles = ["Adwaita++", "Suru++", "Yaru++"];
let styles = ["adwaita", "suru", "yaru"];
let categories = ["documents", "git", "github", "gitlab", "mac", "pictures", "musics", "script", "templates", "videos"];
let colours = ["60spsychodelic", "90ssummer", "adwaita", "blue", "bluegray", "bordeaux", "brown", "canonical", "cyberpunk", "green", "orange", "red", "vermillion", "yellow"];
Adwaita++、Suru++、Yaru++是图标主题的名称,styles是图标的主题,颜色是文件夹颜色。 几乎所有图标主题都有所有类别(相同编号)和相同数量的颜色,但 Adwaita++ 没有文件夹颜色,例如60spsychodelic
和90ssummer
是 Suru++ 独有的。 Suru++ 也没有某些文件夹颜色,例如canonical
和vermillion
。 然后我想排除或删除这些值。
您可以看到字符串titles
的 arrays 或styles
和colours
完全不同。
所以我构建了迭代和三个控制流:
for (let a = 0; a < titles.length; a++)
{
const tt = titles[a];
for (let i = 0; i < styles.length; i++)
{
const style = styles[i];
if (style == "adwaita")
{
html+= '<h1>' + titles[0] + '</h1>';
for (let j = 0; j < categories.length; j++)
{
const category = categories[j];
for (let k = 0; k < colours.length; k++)
{
const colour = colours[k];
html+= "<p><code>"+styles[0]+"-plus/folder-"+category+"-"+colour+".svg</code></p>";
}
}
}
else if (style == "suru")
{
html+= '<h1>' + titles[1] + '</h1>';
for (let j = 0; j < categories.length; j++)
{
const category = categories[j];
for (let k = 0; k < colours.length; k++)
{
const colour = colours[k]
html+= "<p><code>"+styles[1]+"-plus/folder-"+category+"-"+colour+".svg</code></p>";
}
}
}
else
{
html+= '<h1>' + titles[2] + '</h1>';
for (let j = 0; j < categories.length; j++)
{
const category = categories[j];
for (let k = 0; k < colours.length; k++)
{
const colour = colours[k];
html+= "<p><code>"+styles[2]+"-plus/folder-"+category+"-"+colour+".svg</code></p>";
}
}
}
}
}
document.write(html);
现在,让我们将 go 改为:
else if (style == "suru")
{
html+= '<h1>' + titles[1] + '</h1>';
for (let j = 0; j < categories.length; j++)
{
const category = categories[j];
for (let k = 0; k < colours.length; k++)
{
const colour = colours[k]
html+= "<p><code>"+styles[1]+"-plus/folder-"+category+"-"+colour+".svg</code></p>";
}
}
}
在这个迭代colour
中,我尝试编写代码,我从如何从数组中删除特定项目? ,这将删除 Suru++ 中不存在的颜色。
嗯,我想到了:
let colours = ["60spsychodelic=[suru]", "90ssummer=suru]", "adwaita", "blue", "bluegray", "bordeaux", "brown", "canonical", "cyberpunk", "green", "orange", "red", "vermillion=[yaru]", "yellow"];
肯定不错,但是 Adwaita++ 也需要vermillion
,如果我去掉[yaru]
,它最终会被包含在 Suru++ 中,什么不好。
只有这两个问题我无法解决。 虽然我更喜欢虚拟和简单的代码,但你可以自由地编写一个压缩版本,只要你愿意。
你可以查一下就明白了: https://github.com/gusbemacbe/suru-plus-folders/blob/master/languages/en.md
这是准备好的片段供您测试,以发现为什么它重复了 18 倍的值组。
var html = ''; let titles = ["Adwaita++", "Suru++", "Yaru++"]; let styles = ["adwaita", "suru", "yaru"]; let categories = ["documents", "git", "github", "gitlab", "mac", "pictures", "musics", "script", "templates", "videos"]; let colours = ["60spsychodelic", "90ssummer", "adwaita", "blue", "bluegray", "bordeaux", "brown", "canonical", "cyberpunk", "green", "orange", "red", "vermillion", "yellow"]; for (let a = 0; a < titles.length; a++) { const tt = titles[a]; for (let i = 0; i < styles.length; i++) { const style = styles[i]; if (style == "adwaita") { html += '<h1>' + titles[0] + '</h1>'; for (let j = 0; j < categories.length; j++) { const category = categories[j]; for (let k = 0; k < colours.length; k++) { const colour = colours[k]; html += "<p><code>" + styles[0] + "-plus/folder-" + category + "-" + colour + ".svg</code></p>"; } } } else if (style == "suru") { html += '<h1>' + titles[1] + '</h1>'; for (let j = 0; j < categories.length; j++) { const category = categories[j]; for (let k = 0; k < colours.length; k++) { const colour = colours[k]; html += "<p><code>" + styles[1] + "-plus/folder-" + category + "-" + colour + ".svg</code></p>"; } } } else { html += '<h1>' + titles[2] + '</h1>'; for (let j = 0; j < categories.length; j++) { const category = categories[j]; for (let k = 0; k < colours.length; k++) { const colour = colours[k]; html += "<p><code>" + styles[2] + "-plus/folder-" + category + "-" + colour + ".svg</code></p>"; } } } } } document.write(html);
为了解决这个问题,我们应该使用Set()
, Set ; 但我们还必须将我们的Set
转换为普通array
,以便使用join
属性来显示 html。
var html = ''; let titles = ["Adwaita++", "Suru++", "Yaru++"]; let styles = ["adwaita", "suru", "yaru"]; let categories = ["documents", "git", "github", "gitlab", "mac", "pictures", "musics", "script", "templates", "videos"]; let colours = ["60spsychodelic", "90ssummer", "adwaita", "blue", "bluegray", "bordeaux", "brown", "canonical", "cyberpunk", "green", "orange", "red", "vermillion", "yellow"]; let Set_html = new Set(); for (let a = 0; a < titles.length; a++) { const tt = titles[a]; //console.log(a) for (let i = 0; i < styles.length; i++) { const style = styles[i]; if (style == "adwaita") { let fake_colours = JSON.parse(JSON.stringify(colours)) fake_colours.splice(0,2) Set_html.add(`<h1>${titles[0]}</h1>`) for (let j = 0; j < categories.length; j++) { const category = categories[j]; for (let k = 0; k < fake_colours.length; k++) { const colour = fake_colours[k]; Set_html.add(`<p><code>${styles[0]}-plus/folder-${category}-${colour}.svg</code></p>`); } } } else if (style == "suru") { Set_html.add(`<h1>${titles[2]}</h1>`) let fake_colours = JSON.parse(JSON.stringify(colours)) fake_colours.splice(fake_colours.indexOf('canonical'),1) fake_colours.splice(fake_colours.indexOf('vermillion'),1) for (let j = 0; j < categories.length; j++) { const category = categories[j]; for (let k = 0; k < fake_colours.length; k++) { const colour = fake_colours[k]; Set_html.add(`<p><code>${styles[1]}-plus/folder-${category}-${colour}.svg</code></p>`); } } } else { Set_html.add(`<h1>${titles[2]}</h1>`) for (let j = 0; j < categories.length; j++) { const category = categories[j]; for (let k = 0; k < colours.length; k++) { const colour = colours[k]; Set_html.add(`<p><code>${styles[2]}-plus/folder-${category}-${colour}.svg</code></p>`); } } } } } let newArray = Array.from(Set_html).join('') document.write(newArray)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.