React/Typescript 多图

React/Typescript multimap

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

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

interface Details{
    itemArray: Item[]
}

有两种方法可以实现,Maps and indexable types

可索引类型

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

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

Playground

地图

Maps 是一个实现哈希映射的 JS class - 此方法在按 non-string/number 类型进行索引时很有用。

type ItemMap = Map<string, Item[]>

Playground