【发布时间】:2018-08-27 14:42:21
【问题描述】:
由于我的 JS 不太好,我会在这里感谢帮助...
我有这个数组:
segments = [
{ state: 33, color: "red" },
{ state: 66, color:"yellow" },
{ state: 100, color: "green" }
]
三个变量:
goal = 100
currentValue = 26
percentage = currentValue * 100 / goal
我正在尝试编写一个函数来比较对象中的百分比和状态,并返回伴随状态的颜色。例如:
如果百分比
如果百分比在 33 和 66 之间,则必须返回“黄色”。
如果百分比 > 66 或 > 目标,则必须返回“绿色”。
到目前为止我有这个,但它不起作用:
defineChartColor = (currentActivity, goal) => segments.find(segments => segments.state > percentage).color
【问题讨论】:
标签: javascript ecmascript-6 ecmascript-5