【发布时间】:2021-02-12 11:52:02
【问题描述】:
我想从一个单独的数组对象中创建一个字符串数组。
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[]'
如何创建仅包含现有数组的信息字段的字符串数组?
【问题讨论】:
标签: arrays angular typescript for-loop