【问题标题】:how can i find Object data?我怎样才能找到对象数据?
【发布时间】:2021-03-20 06:18:14
【问题描述】:

我在FlatList的TodoItem组件中有多个对象值叫做item。 另外,我有一个名为 itemtow

的对象值

我要做的是将与项目对象的 id 值和itemtow's CommentId 值匹配的项目对象值放入结果常量中。

这是我的数据和代码

项目数据

    item: {id: 163, content: "부모1", createdAt: "2021-03-19T16:23:20.000Z", updatedAt: "2021-03-19T16:23:20.000Z", UserId: 1, …}

    item: {id: 164, content: "부모2", createdAt: "2021-03-19T16:23:23.000Z", updatedAt: "2021-03-19T16:23:23.000Z", UserId: 1, …}

    item: {id: 165, content: "부모3", createdAt: "2021-03-19T16:23:38.000Z", updatedAt: "2021-03-19T16:23:38.000Z", UserId: 2, …}

    item: {id: 166, content: "부모4", createdAt: "2021-03-19T16:23:41.000Z", updatedAt: "2021-03-19T16:23:41.000Z", UserId: 2, …}

itemtow 数据

    itemtow: {id: 268,  CommentId: 166, content: "부모4", active: "1", createdAt: "2021-03-19T16:23:41.000Z", updatedAt: "2021-03-20T05:56:24.000Z", …}

预期的结果数据

    expected result =  {id: 166, content: "부모4", createdAt: "2021-03-19T16:23:41.000Z", updatedAt: "2021-03-19T16:23:41.000Z", UserId: 2, …}

这是我的代码

(todoList.js)

    const TodoList = ({item}) => {

      return (

        <>
        <FlatList
          data={singlePost?.Comments}
          keyExtractor={(item) => String(item.id)}
          
          ListEmptyComponent={<EmptyItem />}
          renderItem={({item}) => (
            <TodoItem
              item={item}
              itemtow={itemtow}
            />
          )}
        />
      </>
      );
    };

    export default TodoList;

(TodoItem.js)

    const TodoItem = ({item ,itemtow}) => {


      const result = item.filter((v) => v.id === itemtow.CommentId);

      console.log("result:",result);

      return (

      )

如何修复我的代码?我试过使用过滤器,但它不起作用。

【问题讨论】:

    标签: javascript node.js reactjs react-native


    【解决方案1】:

    TodoItem 组件中,item 是您传递给FlatList 的数组singlePost?.Comments 中的单个元素(对象)。

    因此,您只需检查item.iditemRow.CommentId

    const TodoItem = ({ item, itemRow }) => {
    
      const matched = item.id === itemRow.CommentId
      console.log('matched? ', matched, item)
    
      return <>...</>
    }
    

    此外,您可能希望将[] 作为备用值传递给FlatList

    <FlatList
      data={singlePost?.Comments ?? []}
      ...
    

    【讨论】:

    • 假设是itemRow 而不是itemtow(错字)。
    【解决方案2】:

    实际上,filter 方法运行良好。

    const items = [
      {id: 163, content: "부모1", createdAt: "2021-03-19T16:23:20.000Z", updatedAt: "2021-03-19T16:23:20.000Z", UserId: 1},
      {id: 164, content: "부모2", createdAt: "2021-03-19T16:23:23.000Z", updatedAt: "2021-03-19T16:23:23.000Z", UserId: 1},
      {id: 165, content: "부모3", createdAt: "2021-03-19T16:23:38.000Z", updatedAt: "2021-03-19T16:23:38.000Z", UserId: 2},
      {id: 166, content: "부모4", createdAt: "2021-03-19T16:23:41.000Z", updatedAt: "2021-03-19T16:23:41.000Z", UserId: 2}
    ];
    
    const itemRow = {id: 268,  CommentId: 166, content: "부모4", active: "1", createdAt: "2021-03-19T16:23:41.000Z", updatedAt: "2021-03-20T05:56:24.000Z"};
    
    const result = items.filter(v => v.id === itemRow.CommentId);
    console.log(result);

    【讨论】:

    • 不,它不起作用;就像我说的那样,项目值不是数组而是对象
    猜你喜欢
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多