[英]How to sort the array of objects on the basis of an element in another array of objects?
[英]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.