javascript - 打字稿从字典和键列表构造一个子字典
问题描述
我正在尝试从字典和键列表创建一个子字典。
type MyDictElementType = {
x: number
}
type MyDictType = {
[key: string]: MyDictElementType
}
const myKeys: string[] = ['a']
const myDict: MyDictType = { 'a': {x: 0}, 'b': {x: 1} }
const createSubDictionary = (fullDictionary: MyDictType, searchKeys: string[]) => {
const newDict: MyDictType = {}
searchKeys.map((searchKey) => {
newDict[searchKey] = fullDictionary[searchKey]
})
return newDict
}
newDict
有没有一种纯粹的函数式方法可以在没有局部变量的情况下实现这一目标?
解决方案
推荐阅读
- jenkins - 詹金斯的声纳扫描仪问题
- angular - 无法在角度 6 的 ng2-smart-table 中获取删除事件
- linkedin - 如何在 android 设备的移动linkedin 应用程序中开始一个新段落?
- java - 如何将 API 访问信息输出到记录器文件中?
- javascript - React + Redux + Babel(...状态意外令牌中的 SyntaxError)
- python - 如何在 One2many 字段或关系表上基于 group by 进行搜索视图
- java - 自定义 JNI 方法链接器或在 Java 应用程序中注册
- java - 初始化 StandardFileSystemManager 的最佳实践是什么
- c++ - 令人不安的评价顺序
- php - 将用户名保存在变量/函数中以备后用