簡體   English   中英

基於帶有對象[JS / TypeScript]的另一個數組的字符串數組

[英]Array with strings on the basis of another array with objects [JS/TypeScript]

我想在帶有對象的現有數組的基礎上用字符串創建新數組,其中Employee類包括firstName字段:

assignees: Array<Employee>;
options: string[];

我嘗試過這種方式:

this.options = this.assignees.forEach(value => value = value.firstName);

但是type 'void' is not assignable to type 'string[]'

有人可以幫忙嗎?

forEach()方法僅對每個數組元素執行一次提供的函數。

您可以嘗試使用map()做到這一點。 我猜這是完成此類任務的正確方法。

// here is how it could look like
this.options = this.assignees.map(value => value.firstName);

map()方法創建一個新數組,並在調用數組中的每個元素上調用提供的函數。

快樂黑客:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM