【问题标题】:React/Typescript multimap反应/打字稿多图
【发布时间】:2020-07-31 17:02:19
【问题描述】:

我是打字稿的新手,不知道如何实现多图。我有如下代码。我需要遍历 itemArray 并根据日期存储 Item。我需要使用日期作为多图的关键。如何在不使用任何外部库的情况下实现这一点?

interface Item {
    id: number;
    date: string;
}

interface Details{
    itemArray: Item[]
}

【问题讨论】:

    标签: reactjs typescript multimap


    【解决方案1】:

    有两种方法可以实现这一点,Mapsindexable types

    可索引类型

    可索引类型是具有类型化键和值的基本对象。:

    interface ItemMap {
        [key: string]: Item[];
    };
    

    Playground

    地图

    Maps 是一个实现哈希映射的 JS 类 - 当按非字符串/数字类型进行索引时,此方法很有用。

    type ItemMap = Map<string, Item[]>
    

    Playground

    【讨论】:

      猜你喜欢
      • 2019-07-11
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 2019-04-25
      • 1970-01-01
      相关资源
      最近更新 更多