arrays - 从对象数组中选择某些项目以在 Typescript 中创建新数组?
问题描述
我想从一个单独的数组对象中创建一个字符串数组。
export interface box{
image: string,
link: string,
button_name: string,
info: string,
description: string
}
export const BOX: box[] = [
{image: 'image here', link: 'google.com',
button_name: 'name', info: 'some information', description: "a description"
},
{image: 'image here again', link: 'another google.com',
button_name: 'another name', info: 'some more information', description: "another description"
},
]
本质上,我想从现有信息中创建一个新数组,但它只是一个信息数组。我不确定如何在打字稿中实现这一点。我试过像这样使用 ForEach 函数:
infos: string[] = BOX.forEach(element => element.info);
但这会给我一个错误,说
Type 'void' is not assignable to type 'string[]'
如何创建仅包含现有数组的信息字段的字符串数组?
解决方案
const infos = BOX.map(element => element.info);
推荐阅读
- javascript - 如何使用reactstrap获取按钮点击输入
- google-sheets - 根据谷歌电子表格中的行和列条件计算值
- ruby-on-rails - 在 Rails 应用程序中本地使用 gem 而无需修改 Gemfile
- solidity - Solidity/Remix:为什么这个函数在分配数组成员时失败?
- swift - 强制展开查询快照,有没有更好的方法来做到这一点?
- ide - 如何让 CLion 在结束命名空间大括号后附加注释?
- android - 错误 || 颤动 || 'owner!._debugCurrentBuildTarget == this': 不正确
- x86 - 英特尔内部函数在字符串中查找匹配字符的位置?
- c++ - 增加递归深度
- stm32 - 停止在 cubeIDE 中使用 HAL