1

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?

1
  • because forEach function doesn't return anything. Use map instead Commented Oct 1, 2018 at 11:22

1 Answer 1

2

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 :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.