for 语句的 Coffeescript 语法问题
Coffeescript syntax issue with for statement
我正在使用 Coffeescript 设置条件,以便在两个变量的图表上设置 fillColor; lastVisibility 和 curr_visibility。我的语法有问题。有什么建议
for set in datasets
line.push
data: set,
lines:
show: true
fill: true
opacity: 0.7
fillColor: if lastVisibilty == 0 & curr_visibility == 0
then "#C90E30"
else lastVisibilty == 0 & curr_visibility == 1
then "#439C32"
else lastVisibilty == 1 & curr_visibility == 0
then "#C90E30"
else
"#439C32"
这里有很多问题:
- 只有在使用
a = if b then c else d
等单行格式时才使用 then
,如果使用 if
等多行格式,则不需要t 使用 then
s.
- 您有多个
else
分支,您的意思是 else if
。
&
和&&
是不同的东西,&
是位运算符,&&
是你要找的逻辑合取。
- 您需要非常小心并保持缩进一致。
将这些调整应用于您的代码,您将获得:
for set in datasets
line.push
data: set,
lines:
show: true
fill: true
opacity: 0.7
fillColor: if lastVisibilty == 0 && curr_visibility == 0
"#C90E30"
else if lastVisibilty == 0 && curr_visibility == 1
"#439C32"
else if lastVisibilty == 1 && curr_visibility == 0
"#C90E30"
else
"#439C32"
但是,循环内的条件逻辑不依赖于 set
(即循环不变),因此您应该将其考虑在内:
fillColor = if lastVisibilty == 0 && curr_visibility == 0
"#C90E30"
else if lastVisibilty == 0 && curr_visibility == 1
"#439C32"
else if lastVisibilty == 1 && curr_visibility == 0
"#C90E30"
else
"#439C32"
for set in datasets
line.push
data: set,
lines:
show: true
fill: true
opacity: 0.7
fillColor: fillColor
或者更好的是,将所有不变的东西推到循环之外以进一步阐明代码:
fillColor = if lastVisibilty == 0 && curr_visibility == 0
"#C90E30"
else if lastVisibilty == 0 && curr_visibility == 1
"#439C32"
else if lastVisibilty == 1 && curr_visibility == 0
"#C90E30"
else
"#439C32"
lines =
show: true
fill: true
opacity: 0.7
fillColor: fillColor
for set in datasets
line.push
data: set,
lines: lines
或者,由于 for 循环是一个表达式,您可以说:
fillColor = if lastVisibilty == 0 && curr_visibility == 0
"#C90E30"
else if lastVisibilty == 0 && curr_visibility == 1
"#439C32"
else if lastVisibilty == 1 && curr_visibility == 0
"#C90E30"
else
"#439C32"
lines =
show: true
fill: true
opacity: 0.7
fillColor: fillColor
line = ({ data: set, lines: line } for set in datasets)
当然假设 line
在你的循环之前是空的;如果不是,那么您可以使用 Array.prototype.concat
:
line = line.concat({data: set, lines: line } for set in datasets)
将循环的数据附加到 line
。
我正在使用 Coffeescript 设置条件,以便在两个变量的图表上设置 fillColor; lastVisibility 和 curr_visibility。我的语法有问题。有什么建议
for set in datasets
line.push
data: set,
lines:
show: true
fill: true
opacity: 0.7
fillColor: if lastVisibilty == 0 & curr_visibility == 0
then "#C90E30"
else lastVisibilty == 0 & curr_visibility == 1
then "#439C32"
else lastVisibilty == 1 & curr_visibility == 0
then "#C90E30"
else
"#439C32"
这里有很多问题:
- 只有在使用
a = if b then c else d
等单行格式时才使用then
,如果使用if
等多行格式,则不需要t 使用then
s. - 您有多个
else
分支,您的意思是else if
。 &
和&&
是不同的东西,&
是位运算符,&&
是你要找的逻辑合取。- 您需要非常小心并保持缩进一致。
将这些调整应用于您的代码,您将获得:
for set in datasets
line.push
data: set,
lines:
show: true
fill: true
opacity: 0.7
fillColor: if lastVisibilty == 0 && curr_visibility == 0
"#C90E30"
else if lastVisibilty == 0 && curr_visibility == 1
"#439C32"
else if lastVisibilty == 1 && curr_visibility == 0
"#C90E30"
else
"#439C32"
但是,循环内的条件逻辑不依赖于 set
(即循环不变),因此您应该将其考虑在内:
fillColor = if lastVisibilty == 0 && curr_visibility == 0
"#C90E30"
else if lastVisibilty == 0 && curr_visibility == 1
"#439C32"
else if lastVisibilty == 1 && curr_visibility == 0
"#C90E30"
else
"#439C32"
for set in datasets
line.push
data: set,
lines:
show: true
fill: true
opacity: 0.7
fillColor: fillColor
或者更好的是,将所有不变的东西推到循环之外以进一步阐明代码:
fillColor = if lastVisibilty == 0 && curr_visibility == 0
"#C90E30"
else if lastVisibilty == 0 && curr_visibility == 1
"#439C32"
else if lastVisibilty == 1 && curr_visibility == 0
"#C90E30"
else
"#439C32"
lines =
show: true
fill: true
opacity: 0.7
fillColor: fillColor
for set in datasets
line.push
data: set,
lines: lines
或者,由于 for 循环是一个表达式,您可以说:
fillColor = if lastVisibilty == 0 && curr_visibility == 0
"#C90E30"
else if lastVisibilty == 0 && curr_visibility == 1
"#439C32"
else if lastVisibilty == 1 && curr_visibility == 0
"#C90E30"
else
"#439C32"
lines =
show: true
fill: true
opacity: 0.7
fillColor: fillColor
line = ({ data: set, lines: line } for set in datasets)
当然假设 line
在你的循环之前是空的;如果不是,那么您可以使用 Array.prototype.concat
:
line = line.concat({data: set, lines: line } for set in datasets)
将循环的数据附加到 line
。