简体   繁体   English

在 JavaScript 中,我无法创建字符串的 arrays 的唯一值,并从另一个数组中排除确定的值

[英]In JavaScript, I can't make unique values of arrays of strings, and exclude determinated values from another array

Only two problems:只有两个问题:

  • I can't make unique values of arrays of strings in iterations and control flow.我无法在迭代和控制流中创建字符串的 arrays 的唯一值。 In my test, the iterations and control flow duplicated the values 18 times, and I tried to change a lot of codes in different way, but unsuccessfully.在我的测试中,迭代和控制流重复了 18 次值,我尝试以不同的方式更改很多代码,但没有成功。
  • I learned from How can I remove a specific item from an array?我从中学到了如何从数组中删除特定项目? , but my case is different, because two associative folders are different. ,但我的情况不同,因为两个关联文件夹不同。 I will explain:我会解释:
  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++ and Yaru++ are names of icons themes, styles are themes of icons, and colours are folders colours. Adwaita++、Suru++、Yaru++是图标主题的名称,styles是图标的主题,颜色是文件夹颜色。 Almost all icons themes have all categories (the same number), and the same number of colours, but Adwaita++ does not have folder colours, for example, 60spsychodelic and 90ssummer , which are exclusive of Suru++.几乎所有图标主题都有所有类别(相同编号)和相同数量的颜色,但 Adwaita++ 没有文件夹颜色,例如60spsychodelic90ssummer是 Suru++ 独有的。 Suru++ also does not have certain folders colours, for example, canonical and vermillion . Suru++ 也没有某些文件夹颜色,例如canonicalvermillion Then I want to exclude or remove the values.然后我想排除或删除这些值。

You can see that the arrays of strings titles or styles and colours are totally different.您可以看到字符串titles的 arrays 或stylescolours完全不同。

So I built iterations and three control flows:所以我构建了迭代和三个控制流:

 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);

Now, let's go to:现在,让我们将 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>";
    }
  }  
}

In this iteration colour , I tried to write the codes, that I got from How can I remove a specific item from an array?在这个迭代colour中,我尝试编写代码,我从如何从数组中删除特定项目? , that would remove the colours that do not exist in Suru++. ,这将删除 Suru++ 中不存在的颜色。

Well, I thought of:嗯,我想到了:

let colours = ["60spsychodelic=[suru]", "90ssummer=suru]", "adwaita", "blue", "bluegray", "bordeaux", "brown", "canonical", "cyberpunk", "green", "orange", "red", "vermillion=[yaru]", "yellow"];

It must be good, but Adwaita++ also needs vermillion , and if I remove [yaru] , it will end up being included in Suru++, what is not good.肯定不错,但是 Adwaita++ 也需要vermillion ,如果我去掉[yaru] ,它最终会被包含在 Suru++ 中,什么不好。

Only these two problems I can not fix.只有这两个问题我无法解决。 Although I prefer a dummy and simple code, you are free to write a condensed version only if you want.虽然我更喜欢虚拟和简单的代码,但你可以自由地编写一个压缩版本,只要你愿意。

You can check and will understand: https://github.com/gusbemacbe/suru-plus-folders/blob/master/languages/en.md你可以查一下就明白了: https://github.com/gusbemacbe/suru-plus-folders/blob/master/languages/en.md

Here is the ready snnipet for you to test for discovering why it duplicated 18 times the group of values.这是准备好的片段供您测试,以发现为什么它重复了 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);

for solving this problem we should use Set() , Set ;为了解决这个问题,我们应该使用Set()Set But also we have to convert our Set to a normal array in order to use the join property for showing the html.但我们还必须将我们的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.

相关问题 javascript从数组中的所有数组中查找唯一值 - javascript find unique values from all arrays in array Javascript / Lodash-在数组数组中获取唯一值 - Javascript/Lodash - Getting unique values in an Array of arrays Javascript - Array - 将值从一个数组分配给另一个数组中的字符串 - Javascript - Array - Assigning values from one array to strings in another array 将唯一值从数组数组合并到数组 - Merge unique values to array from array of arrays 如何将 arrays 数组从字符串转换为 Javascript 中的数字? - How can i turn an array of arrays from strings to numbers in Javascript? 通过比较另一个数组中的值在JavaScript中创建数组 - Creating arrays in JavaScript by comparing values in another array 如何在对象数组中获取具有唯一值的键? (JavaScript) - How can I get keys with unique values in an array of objects? (JavaScript) 来自唯一值数组的唯一随机值javascript - Unique random values from array of unique values javascript 如何通过从单独的数组中匹配的字符串对象值从 3 个嵌套的 json 数组中提取对象值,然后在 html/javascript 中显示它 - How can I extract object values from 3 nested json arrays by matched string object values from a separate array and then display it in html/javascript 如何从一个对象创建一个字符串数组,其中各个键的值是单独的字符串数组? - How do I create an array of strings from an object where the values of individual keys are separate arrays of strings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM