如何将图标显示给特定的数字。

huangapple go评论56阅读模式
英文:

how to display an icon to a particular number given

问题

请我想根据给定的数字显示一个图标。例如,如果给定的数字是3,则显示3个图标。请问我如何在TypeScript中实现这一点。

以下是我的代码:

const icons = <MdStar />
const displayIcon = (number: any) => {
    let random: any = Math.floor((Math.random() * 4) + 1);
    if (random > 0) {
        number.length === random
    }
    console.log(number);
};

displayIcon(icons)

这是您提供的代码的翻译部分。如果您需要进一步的帮助或解释,请随时提出。

英文:

please i want to display an icon according to a number given. for instance if the given number is 3 let 3 icon be display. please how can achieve this in TypeScript.

here is my code

const icons = &lt;MdStar /&gt;
    const displayIcon = (number:any) =&gt; {
        let random: any = Math.floor((Math.random() * 4) + 1);
        if(random &gt; 0 ){
            
            number.length === random
        }
        console.log(number);
    };

    displayIcon(icons)

答案1

得分: 1

你可以通过创建一个randomNumber,然后从这个数字创建一个数组。最后,遍历这个数组并返回MdStar组件。

function YourComponent() {
  const randomNumber: number = Math.floor(Math.random() * 4 + 1);

  return (
    <>
      {Array.from({ length: randomNumber }, (_, i) => i).map((_, i) => (
        <MdStar key={i} />
      ))}
    </>
  );
}
英文:

You can do this by creating a randomNumber and from the number you can create a array. Finally map over the array and return the MdStar component.

function YourComponent() {
  const randomNumber: number = Math.floor(Math.random() * 4 + 1);

  return (
    &lt;&gt;
      {Array.from({ length: randomNumber }, (_, i) =&gt; i).map((_, i) =&gt; (
        &lt;MdStar key={i} /&gt;
      ))}
    &lt;/&gt;
  );
}

huangapple
  • 本文由 发表于 2023年2月6日 18:38:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定