数组
为什么这个单独拉出来讲一下,因为我们要经常用到数组
我们在与后端交互的时候,经常要处理数组,那么我们该怎么定义数组结构呢?
后端返回的数据结构如下
[{
"id": 397,
"roleName": "超级管理员1986",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-24T13:16:43.000000Z"
}, {
"id": 396,
"roleName": "编辑197",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-23T01:47:22.000000Z"
}, {
"id": 395,
"roleName": "超级管理员197",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-23T01:47:22.000000Z"
}, {
"id": 394,
"roleName": "编辑196",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-23T01:47:22.000000Z"
}, {
"id": 393,
"roleName": "超级管理员196",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-23T01:47:22.000000Z"
}, {
"id": 392,
"roleName": "编辑195",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-23T01:47:22.000000Z"
}, {
"id": 391,
"roleName": "超级管理员195",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-23T01:47:22.000000Z"
}, {
"id": 390,
"roleName": "编辑194",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-23T01:47:22.000000Z"
}, {
"id": 389,
"roleName": "超级管理员194",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-23T01:47:22.000000Z"
}, {
"id": 388,
"roleName": "编辑193",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-23T01:47:22.000000Z"
}, {
"id": 387,
"roleName": "超级管理员193",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-23T01:47:22.000000Z"
}, {
"id": 386,
"roleName": "编辑192",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-23T01:47:22.000000Z"
}, {
"id": 385,
"roleName": "超级管理员192",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-23T01:47:22.000000Z"
}, {
"id": 384,
"roleName": "编辑191",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-23T01:47:22.000000Z"
}, {
"id": 383,
"roleName": "超级管理员191",
"createdAt": "2020-10-23T01:47:22.000000Z",
"updatedAt": "2020-10-23T01:47:22.000000Z"
}]
定义接口
interface IRole {
id: number
roleName: string
createdAt: string
updatedAt: string
}
定义数组
// jsonStr为你的json数据
const roleList: IRole[] = <Array<IRole>>JSON.parse(jsonStr)
console.log(roleList)
总结一下,我们用到ts中的知识
- 接口
- 类型断言