简体   繁体   English

如何将对象从数组分配给箭头函数?

[英]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. 我正在尝试在Redux状态下从数组分配具有对象属性的变量。 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. 我试图遍历对象数组并在项目的ID匹配我要搜索的ID时分配变量。

I have been trying anything I can from nested if statements, multiple returns, I cannot seem to figure this out. 我一直在尝试从嵌套if语句,多次返回中进行的所有尝试,但似乎无法弄清楚。

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). todos是我要搜索的数组,而itemID是我要寻找的ID(都是redux状态的一部分)。

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). 因此,我试图在模态文件中为变量分配当前待办事项(数组中的对象)的所有属性。

The find function expects that you'll return True when you find your item. find函数期望您在找到商品时将返回True。 also, you need to specify a 'return' statement. 另外,您需要指定“ return”语句。

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

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} 那么您可以使用与{currentProps}类似的渲染方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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