如何在TypeScript中创建包含指定对象数组的二维数组?
Got it! Creating a 2D array that holds your appChartData and versionChartData is really straightforward—you just need to wrap both existing arrays inside another array. Here's how to do it cleanly in TypeScript, including optional type safety for clarity:
Step 1: Define the 2D Array Type (Optional but Recommended)
If you want explicit type annotations to avoid type confusion later, you can define the structure of your 2D array first:
type CombinedChartData = [ Array<{date: string, total: number, some_usage: number}>, Array<{date: string, ver1sion: number, version2: number}> ];
Step 2: Build the 2D Array
Now simply assign your two arrays to form the 2D structure:
// Assuming your arrays are already populated with data const combined2DArray: CombinedChartData = [appChartData, versionChartData];
Shorter Alternative (No Explicit Type)
If you don't need the strict type definition, you can do this in one quick line:
const combined2DArray = [appChartData, versionChartData];
Now combined2DArray[0] will reference your appChartData set, and combined2DArray[1] will point to versionChartData—perfect for passing both datasets to components or functions that expect a 2D array input!
内容的提问来源于stack exchange,提问作者Kim2018

