React: Find a prop in props array


#1

I have a component named ChangeTask that has this.props.tasks is an array and a this.props.params.taskId. How to get a task that has the _id = taskId? I tried this this.props.tasks.find(task => task._id === this.props.params.taskId) but it returns undefined. Thank you.


#2

Watch out for this gotchas…
Don’t use arrow functions without parentheses

const id = this.props.params.taskId;
const result = this.props.tasks.find(task => (task._id === id));

should propably work.
In your version, the this inside the test function is the global object or undefined.

You could do also
const result = this.props.tasks.find(task => (task._id === this.props.params.taskId), this);


#3

That solved it, thank you!