angular - 打字稿:将字符串 [] 映射到 [字符串,字符串]
问题描述
如何将字符串数组映射string[]
到带有字符串的二维数组[string, string]
?
我试过了
const animals: string[] = ['Dog', 'Cat', 'Mouse'];
// This throws an error
let splittedArray: [string, string] = animals.slice(0,2);
// This doesn't throw an error
// let splittedArray: [string, string] = ['Dog', 'Cat'];
// Error Message
// Type 'string[]' is missing the following properties from type '[string, string]': 0, 1 ts(2739)
我想要的输出应该是:
console.log(splittedArray);
// ['Dog', 'Cat']
解决方案
你想要一个二维数组还是一个有 2 个元素的数组?这似乎适用于您的输出:
const animals: string[] = ['Dog', 'Cat', 'Mouse'];
let splittedArray: string[] = animals.slice(0,2);
推荐阅读
- python - 如何从python中的文本文件中删除特定行
- c# - 是否可以在锁内使用另一个线程来重定向文件中的输出?
- node.js - Npm 找不到文件 - 安装 bcrypt
- context-free-grammar - 查找给定语言的上下文无关语法
- python - 从“azure.storage.blob”导入“BlobServiceClient”时出错
- google-sheets - 为什么 Google 表格中的 0 有时是绿色有时是红色?
- android - 找不到变量 DeviceEventEmitter
- c# - 如何使用 Roslyn 操作 CSHTML
- python - cx-Freeze 无法构建“LookupError:未知编码”
- flurl - Flurl 不遵守超时设置