2017-08-16 82 views
1

我想提高我的一些JS技能。更好的方法來做到這一點,如果語句

我有許多十六進制顏色,它們存儲在一個存儲爲前景色和背景色的對象中。

當網頁的寬度是一個特定的大小,我想確保一些特定的顏色不被用來改變文字顏色。

如何繼承我到目前爲止。這工作正常......我知道我也可以使用開關,但其他人可以改善if ||請說明一下嗎?

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; 

      } 
     }' 

謝謝

+0

這樣的if語句是什麼原因? – evolutionxbox

+0

可能的重複[如何檢查數組是否包含JavaScript中的對象?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an- object-in-javascript) – jimf

+0

回答幾個小時後的類似問題https://stackoverflow.com/questions/45711665/shorter-syntax-of-conditional-or-against-many-strings –

回答

0

你可以嘗試用Array#filter

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

您可以創建一個數組,包括所有的顏色,然後檢查所選擇的顏色包括該陣列英寸

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

0

你可以使用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; 
 
}

隨着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!'); 
 
}

0

if語句不要使用。它們是代碼味道。

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]; 
相關問題