how can i find Object data?

Viewed 54

I have multiple object values ​​called items in the TodoItem component in FlatList. also, I have a single object value called itemtow

What I'm trying to do is to put the item object value that matches the item object's id value and the itemtow's CommentId value into the result constant.

this is my data and code

item data

    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 data

    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 data it would be

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

this is my code

(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 (

      )

how can i fix my code? i've tried use filter but it doesn't work .

2 Answers

In the TodoItem component, item is a single element (object) from the array singlePost?.Comments which you passed to FlatList.

So, you simply need to check the item.id against itemRow.CommentId:

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

  const matched = item.id === itemRow.CommentId
  console.log('matched? ', matched, item)

  return <>...</>
}

Also, you may want to pass [] as fallback value to FlatList:

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

Actually, the filter method is working fine.

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);

Related