goolius_boozler
goolius_boozler

Reputation: 253

How to assign an object from an array to an arrow function?

I am trying to assign a variable with the properties of an object from an array in my redux state. I am trying to loop through the array of objects and assign the variable when the ID of the item matches up to the ID that I am searching for.

I have been trying anything I can from nested if statements, multiple returns, I cannot seem to figure this out.

Currently, this is what I have.

const currItemProps = () => {
        this.props.todos.find((todo) => {
        (todo.id === this.props.itemID) ?
          { todo } : null
        }
      );
    };

todos is my array I am searching for and the itemID is the ID I am lookking for (both are pieces of redux state).

I am trying to open a modal on the press of a todo that has the properties of the todo. Hence I am trying to assign a variable in my modal file with all of the properties of its current todo (the object from the array).

Upvotes: 0

Views: 76

Answers (2)

Garry
Garry

Reputation: 534

In case you directly want to return model

const currItemProps = () => {
        this.props.todos.find((todo) => {
        (todo.id === this.props.itemID) ?
          <ComponentName todoProps={todo}/> : null
        }
      );
    };

then you can use the same in render method like {currentProps}

Upvotes: 0

Adi Darachi
Adi Darachi

Reputation: 2175

The find function expects that you'll return True when you find your item. also, you need to specify a 'return' statement.

const currItemProps = () => {
       return this.props.todos.find((todo) => todo.id === this.props.itemID);
};

Upvotes: 2

Related Questions