这样定义export type Site = {
workbooks: {
workbook: {
project:{
id: string;
}
}[];
}
}
或type Workbook = {
project:{
id: string;
}
};
export type Site = {
workbooks: {
workbook: Workbook[]
}
}
补充个使用Array.filter过滤的以及ts类型定义type WorkBook = {
project:{
id:string;
name:string;
},
owner:{
id:string;
name:string;
},
tags:{[key:string]:string};
dataAccelerationConfig:{
accelerationEnabled:boolean;
},
id: string;
name:string;
description: string;
contentUrl: string;
webpageUrl: string;
showTabs:string;
size: string;
createdAt: string;
updatedAt: string;
encryptExtracts:string;
defaultViewId: string;
};
type WorkBookQuerResult = {
pagination: {
pageNumber: string;
pageSize: string;
totalAvailable: string;
},
workbooks:{
workbook:WorkBook[];
}
}
let dataResult:WorkBookQuerResult = {
"pagination": {
"pageNumber": "1",
"pageSize": "100",
"totalAvailable": "17"
},
"workbooks": {
"workbook": [
{
"project": {
"id": "fd43940e-9581-11ea-b965-a72ac3f8c8fb",
"name": "默认值"
},
"owner": {
"id": "5a328439-9b3d-4197-ab4c-f334839061f7",
"name": "admin"
},
"tags": {},
"dataAccelerationConfig": {
"accelerationEnabled": false
},
"id": "14def8ac-ae8b-42ee-8c22-723827f5022e",
"name": "工作簿 1",
"description": "",
"contentUrl": "1",
"webpageUrl": "http://111.*.*.*:63192/#/workbooks/3",
"showTabs": "true",
"size": "1",
"createdAt": "2020-07-30T01:46:11Z",
"updatedAt": "2020-08-07T09:43:07Z",
"encryptExtracts": "false",
"defaultViewId": "6a2188bf-33d8-4343-a934-a6b74bbe5f6f"
},
]
}
};
function filterWith(workbookList:WorkBook[],siteId:string){
return workbookList.filter((someworkbook:WorkBook)=>{
if(someworkbook.project.id===siteId){
return true;
}
return false;
});
}
let requiredSiteId = 'fd43940e-9581-11ea-b965-a72ac3f8c8fb';
let resultWorkBookList = filterWith(dataResult.workbooks.workbook,requiredSiteId);
console.log('resultWorkBookList:',resultWorkBookList);