React Js中循环ant设计标签颜色

发布于 2025-01-17 04:07:40 字数 769 浏览 3 评论 0原文

我使用 Ant Design 和来自 API 的数据。我假设像这样的数据

data = [
  {
     name: "John",
     job: "Freelancer",
  },
  {
     name: 'Bob',
     job: 'UI Designer'
  },
  {
     name: 'Sam',
     job: 'CEO'
  },
  {
     name: 'Alex',
     job: 'Mobile Dev'
  },
  {
     name: 'Jess',
     job: 'Web Dev'
  },
];

我想用 Ant Design 的标签返回作业,该标签具有不同的颜色

<Tag color="green">green</Tag>
<Tag color="cyan">cyan</Tag>

我已经循环了数据。但我不知道如何使数据具有不同的颜色标签

data.map((el) => {
//This example doesn't have color
  return <Tag>{el.job}</Tag>
})

如何实现?抱歉我的英语不够好。我希望你明白我的意思 或者您可以访问代码沙箱此处

I use Ant Design and data which coming from API. I assume the data like this

data = [
  {
     name: "John",
     job: "Freelancer",
  },
  {
     name: 'Bob',
     job: 'UI Designer'
  },
  {
     name: 'Sam',
     job: 'CEO'
  },
  {
     name: 'Alex',
     job: 'Mobile Dev'
  },
  {
     name: 'Jess',
     job: 'Web Dev'
  },
];

I want to return the job with Tag from Ant Design which the tag has a different color

<Tag color="green">green</Tag>
<Tag color="cyan">cyan</Tag>

I have looped the data. but I don't know how to make the data have a different color tag

data.map((el) => {
//This example doesn't have color
  return <Tag>{el.job}</Tag>
})

How to achieve that ? And sorry i'm not good enough in English. I hope you understand what i mean
Or you can visit code sandbox here

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

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

发布评论

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

评论(1

我最亲爱的 2025-01-24 04:07:40

在您的数据中,为每个对象添加一个类似 tagColor 的属性。

data = [
    {
     name: "John",
     job: "Freelancer",
     tagColor: "red"
    },
    {
     name: 'Bob',
     job: 'UI Designer'
     tagColor: "green"
    },
    {
     name: 'Sam',
     job: 'CEO'
     tagColor: "blue"
    },
];

然后在循环中,使用该属性动态添加颜色。就像,

data.map((el) => {
  return <Tag color={el.tagColor}>{el.job}</Tag>
});

更新

如果颜色可以是随机的,您可以将所有颜色放入一个数组中。您可以使用索引甚至随机化器逐一选择颜色。就像,

const colors = ["red", "blue", "green"];
data.map((el, i) => {
  return <Tag color={colors[(i%colors.length)]}>{el.job}</Tag>
});

它会根据索引在数组中一一挑选颜色。

In your data, add a property something like tagColor for each object.

data = [
    {
     name: "John",
     job: "Freelancer",
     tagColor: "red"
    },
    {
     name: 'Bob',
     job: 'UI Designer'
     tagColor: "green"
    },
    {
     name: 'Sam',
     job: 'CEO'
     tagColor: "blue"
    },
];

Then in the loop, use that property to dynamically add colors. Like,

data.map((el) => {
  return <Tag color={el.tagColor}>{el.job}</Tag>
});

Updated

If the colors can be random, you can place all your colors in an array. And you can pick colors one by one using index or even a randomiser. Something like,

const colors = ["red", "blue", "green"];
data.map((el, i) => {
  return <Tag color={colors[(i%colors.length)]}>{el.job}</Tag>
});

It will pick colors in the array one by one based on index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文