简体   繁体   中英

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

I would like to create new array with strings on the basis of existing array with objects, where Employee class includes firstName field:

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

I tried do this in this way:

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

But type 'void' is not assignable to type 'string[]'

Could someone please assist?

The forEach() method just executes a provided function once for each array element.

You could try to do it with map() . That's I guess is the right way to do such tasks.

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

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

Happy hacking :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM