简体   繁体   English

Map 数组元素转换为 typescript 中的属性

[英]Map array elements into properties in typescript

I have a 3rd party service that returns an array of size four arrays.我有一个第 3 方服务,它返回一个大小为 4 的数组 arrays。

The array is structured like该数组的结构类似于

[
 [Time, Min, Max, Average],
 [Time, Min, Max, Average],
 etc
]

Is there a way to implicitly define an interface such that I can deal with properties when referring to this data?有没有办法隐式定义一个接口,以便我可以在引用这些数据时处理属性?

something like就像是

interface ApiResult {
  ItemArray: Item[]
}

interface Item {
  Time: string,
  Min: string,
  Max: string,
  Average: string
}

It is not possible to define an interface because the array holds elements of primitive type string (Came to know about the type from your example interface) and not an object.无法定义接口,因为该数组包含原始类型string的元素(从您的示例接口了解类型)而不是 object。 It is simply calling the array as an array of type string.它只是将数组称为字符串类型的数组。 For the outer array which is holding all these arrays, we can call it as an array that holds arrays of type string, that is:对于包含所有这些 arrays 的外部数组,我们可以将其称为包含字符串类型的 arrays 的数组,即:

var data: Array<Array<string>> = [
 ["Time", "Min", "Max", "Average"],
]

If your array is holding an object with those four properties, then you can specify the type like:如果您的阵列包含具有这四个属性的 object,那么您可以指定如下类型:

interface Item {
  Time: string,
  Min: string,
  Max: string,
  Average: string
}

var data: Array<Array<Item>> = [
 [{"Time": "Time", "Min": "Min", "Max": "Max", "Average": "Average"}],
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM