简体   繁体   English

更好的if语句

[英]Better way to do this if statement

I am trying to improve some of my JS skills. 我正在尝试提高一些JS技能。

I have a number of colours in hex which are in an object stored as foreground colours and background colours. 我有一些十六进制颜色,这些颜色存储在作为前景色和背景色存储的对象中。

When the width of the webpage is a particular size i want to make sure that some specific colours are NOT used from the object to change the text colour. 当网页的宽度为特定大小时,我要确保未从对象中使用某些特定颜色来更改文本颜色。

How heres what I have so far. 到目前为止,我的情况如何。 This works fine...and i know i could use a switch too but can anyone else improve the if || 这很好用...而且我知道我也可以使用开关,但是如果||,其他人可以改善吗? statemenent please? 请声明

 var colorThemes = [
        // Combo 1:
        { foreground: "#0A1C6B", background: "#5DF0AD" },
        // Combo 2:
        { foreground: "#C2F5FF", background: "#0A1C6B" },
        // Combo 3:
        { foreground: "#583985", background: "#CCCCF0" },
        // Combo 4:
        { foreground: "#FBBEA6", background: "#5839B5" },
        // Combo 5:
        { foreground: "#8A350D", background: "#FFDB0D" }
    ]

   var randomnumber = Math.floor((Math.random() * colorThemes.length));

   var selector = colorThemes[randomnumber]

   if (selector.foreground === "#0A1C6B" || selector.foreground === "#5DF0AD" || selector.foreground === "#C2F5FF" || selector.foreground === "#ABD1fA" || selector.foreground === "#FBBEA6" || selector.foreground === "#FACCD4" || selector.foreground === "#FF5919" ||  selector.foreground === "#D9F2AD" || selector.foreground === "#83BF25"){

            copyCount[i].style.color = selector.background;

            }
        }'

Thank you 谢谢

You can create an array including all the colors and then check that the selected color is included in this array. 您可以创建一个包含所有颜色的数组,然后检查所选颜色是否包含在该数组中。

 const colorThemes = [ { foreground: "#0A1C6B", background: "#5DF0AD" }, { foreground: "#C2F5FF", background: "#0A1C6B" }, { foreground: "#583985", background: "#CCCCF0" }, { foreground: "#FBBEA6", background: "#5839B5" }, { foreground: "#8A350D", background: "#FFDB0D" } ] const colors = colorThemes.reduce((acc, curr) => { acc.push(curr.foreground, curr.background); return acc; }, []); const randomnumber = Math.floor((Math.random() * colorThemes.length)); const selector = colorThemes[randomnumber] if (colors.includes(selector.foreground)) { console.log('Success'); } 

You could try with Array#filter 您可以尝试使用Array#filter

if (colorThemes.filter(a => (selector.foreground === a.foreground|| selector.foreground == a.background)).length > 0) {
  copyCount[i].style.color = selector.background;
} }

You could use Array#indexOf with an array for the colors. 您可以将Array#indexOf与颜色数组一起使用。

 var colorThemes = [{ foreground: "#0A1C6B", background: "#5DF0AD" }, { foreground: "#C2F5FF", background: "#0A1C6B" }, { foreground: "#583985", background: "#CCCCF0" }, { foreground: "#FBBEA6", background: "#5839B5" }, { foreground: "#8A350D", background: "#FFDB0D" }], randomnumber = Math.floor(Math.random() * colorThemes.length), selector = colorThemes[randomnumber], changeColors = ["#0A1C6B", "#5DF0AD", "#C2F5FF", "#ABD1fA", "#FBBEA6", "#FACCD4", "#FF5919", "#D9F2AD", "#83BF25"]; if (changeColors.indexOf(selector.foreground) !== -1) { console.log('change color!'); //copyCount[i].style.color = selector.background; } 

With ES6 you could use Set for the colors 使用ES6,您可以使用Set作为颜色

 var colorThemes = [{ foreground: "#0A1C6B", background: "#5DF0AD" }, { foreground: "#C2F5FF", background: "#0A1C6B" }, { foreground: "#583985", background: "#CCCCF0" }, { foreground: "#FBBEA6", background: "#5839B5" }, { foreground: "#8A350D", background: "#FFDB0D" }], randomnumber = Math.floor(Math.random() * colorThemes.length), selector = colorThemes[randomnumber], colorSet = new Set(["#0A1C6B", "#5DF0AD", "#C2F5FF", "#ABD1fA", "#FBBEA6", "#FACCD4", "#FF5919", "#D9F2AD", "#83BF25"]); if (colorSet.has(selector.foreground)) { console.log('change color!'); //copyCount[i].style.color = selector.background; } else { console.log('nothing has changed!'); } 

Don't use an if statement. 不要使用if语句。 They are code-smells. 它们是代码气味。

var colorThemes = {
  "#0A1C6B": "#5DF0AD"
  , "#C2F5FF": "#0A1C6B"
  , "#583985": "#CCCCF0"
  , "#FBBEA6": "#5839B5"
  , "#8A350D": "#FFDB0D"
};

var foregroundColors = Object.keys(colorThemes);
var randomForegroundIdx = Math.floor(Math.random() * colorThemes.length);
var randomForeground = foregroundColors[randomForegroundIdx];
copyCount[i].style.color = colorThemes[randomForeground];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM