簡體   English   中英

如何將對象從數組分配給箭頭函數?

[英]How to assign an object from an array to an arrow function?

我正在嘗試在Redux狀態下從數組分配具有對象屬性的變量。 我試圖遍歷對象數組並在項目的ID匹配我要搜索的ID時分配變量。

我一直在嘗試從嵌套if語句,多次返回中進行的所有嘗試,但似乎無法弄清楚。

目前,這就是我所擁有的。

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

todos是我要搜索的數組,而itemID是我要尋找的ID(都是redux狀態的一部分)。

我試圖在具有待辦事項屬性的待辦事項的新聞上打開模式。 因此,我試圖在模態文件中為變量分配當前待辦事項(數組中的對象)的所有屬性。

find函數期望您在找到商品時將返回True。 另外,您需要指定“ return”語句。

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

如果您直接想退貨

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

那么您可以使用與{currentProps}類似的渲染方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM