[英]How to change the background color of a cell based on its value using javascript?
[英]How to change color for the numbers based on its value using javascript and react?
我想使用 javascript 根据其值将 colors 应用于变量计数。
我想做什么? 变量 count 的值是动态的,它可以具有小于 0 或大于 0 的值。
所以现在当计数值为
0 color should be red
1 color should be yellow
greater than 1 color should be green
less than 0 meaning negative integers like -1, -2 so on color should be grey
我有下面的代码,它根据值改变颜色。
const countColor = (count: number) => {
const colors = [red, yellow];
return colors[count] || green;
};
因此,上面的代码适用于计数值为 0、1 和大于 1 的值。我如何更改上面的代码,以便在值小于 0 时处理或将计数值更改为灰色
有人可以帮我解决这个问题。 谢谢。
一个简单的方法是忘记[red, yellow]
数组,只处理 count 以返回颜色:
const countColor = (count: number) => {
if (count < 0) return grey;
else if (count > 1) return green;
else if (count === 0) return red;
else if (count === 1) return yellow;
};
使用三元运算符,如果count
小于零则返回grey
,否则返回colors[count]
或如果colors[count]
undefined
则返回green
const countColor = (count: number) => {
const colors = ['red', 'yellow'];
return count < 0 ? 'grey' : (colors[count] || 'green');
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.