React中的阵列循环的响应值

发布于 2025-02-05 17:24:32 字数 378 浏览 3 评论 0原文

我想根据响应的值显示颜色。 来自响应的数据>

如果优先级很高,则卡颜色为红色,并且优先级低的卡颜色将为绿色

<a style={{ background: 'red' }}>{el.priority}</a>

我将如何实现:

{el.priority.high}
{el.priority.low}
{el.priority.mild}
{el.priority.blocker}

I want to display the color of according to the value from Response.
data from response

If the priority is high, the card color will be red, and if the priority is low the card color will be green

<a style={{ background: 'red' }}>{el.priority}</a>

How I will achieve this:

{el.priority.high}
{el.priority.low}
{el.priority.mild}
{el.priority.blocker}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风无影 2025-02-12 17:24:33

@Aman的答案是正确的。如果背景属性不起作用。您可以更具体地使用backgrowdColor进行操作

<a style={{ backgroundColor: el.priority === "high" ? "red" : "green" }}>{el.priority}</a>

,并且如果优先级超过2个值,则可以在每种类型上映射颜色,并且使用索引操作员从对象中获取颜色,

const bgColors = { 
            high: 'red', 
            low: 'green', 
            medium: 'yellow', 
            none: 'black', 
            default: 'blue'
};

<a style={{ backgroundColor: bgColors[el.priority] }}>{el.priority}</a>

即建议使用链接从<<<<代码> react-native 或@react-navigation/本机而不是计划锚标签。纯锚标签应仅用于绝对路径。

@Aman's answer is correct. In case background property doesn't work. You can do it more specifically using backgroundColor

<a style={{ backgroundColor: el.priority === "high" ? "red" : "green" }}>{el.priority}</a>

And if you have priority more than 2 values you can map color on each type and the use index operator to get color from object i.e

const bgColors = { 
            high: 'red', 
            low: 'green', 
            medium: 'yellow', 
            none: 'black', 
            default: 'blue'
};

<a style={{ backgroundColor: bgColors[el.priority] }}>{el.priority}</a>

It is recommended to use Link from react-native or @react-navigation/native instead of plan anchor tag. pure anchor tag should be only use for absolute paths.

薆情海 2025-02-12 17:24:33

您需要使用条件渲染

 <a style={{ background: el.priority === "high" ? "red" : "green" }}>{el.priority}</a>

You need to use conditional rendering

 <a style={{ background: el.priority === "high" ? "red" : "green" }}>{el.priority}</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文