我正在尝试在我的React应用程序中实现一个点赞系统,但是为此,我需要知道currentLoggedIn用户之前喜欢的帖子,以便显示不喜欢的图标,反之亦然。

我的第一个想法是通过创建一个小的函数来接收两个参数,loginIn用户和postId。这是功能:

const clickLike = (userId, postId) => {
    const match =
      timeline &&
      timeline.map(
        post =>
          post._id === postId && post.likes.filter(user => user === userId)
      );
    return match;
  };


以前的代码应该映射到所有帖子中,如果loginIn用户已经在postId对象的顶部,则返回true。这是我的JSON的样子:

"data": [
  {
      "status": "published",
      "_id": "5e5dcfc65104666e70558110",
      "text": "Mierda!",
      "user": {
          "_id": "5e0925629648903308163aeb",
          "username": "GodHimself",
          "avatar": "//www.gravatar.com/avatar/b62ddb6758fca61fb7a56381ac7cb07d?s=200&r=pg&d=mm",
          "bio": "I'm not God",
          "id": "5e0925629648903308163aeb"
      },
      "likes": [
          {
              "_id": "5e76bc5fd3266e125c2dc4b9",
              "user": "5e0925629648903308163aeb"
          },
      ],
      "id": "5e5dcfc65104666e70558110"
  },
  {
      "status": "published",
      "_id": "5e4a08dc4c1f0d2394f28d8e",
      "text": "Initial D!",
      "user": {
          "_id": "5e0925629648903308163aeb",
          "username": "GodHimself",
          "avatar": "//www.gravatar.com/avatar/b62ddb6758fca61fb7a56381ac7cb07d?s=200&r=pg&d=mm",
          "bio": "I'm not God",
          "id": "5e0925629648903308163aeb"
      },
      "likes": [],
      "id": "5e4a08dc4c1f0d2394f28d8e"
  },
  {
      "status": "published",
      "_id": "5e4a08584c1f0d2394f28d8d",
      "text": "Berserk Bois!",
      "user": {
          "_id": "5e0925629648903308163aeb",
          "username": "GodHimself",
          "avatar": "//www.gravatar.com/avatar/b62ddb6758fca61fb7a56381ac7cb07d?s=200&r=pg&d=mm",
          "bio": "I'm not God",
          "id": "5e0925629648903308163aeb"
      },
      "likes": [
          {
              "_id": "5e4b5c889861ad1cf8b67bbb",
              "user": "5e0925629648903308163aeb"
          },
          {
              "_id": "5e4b5ca59861ad1cf8b67bbc",
              "user": "5e0925629648903308163aeb"
          }
      ],
      "id": "5e4a08584c1f0d2394f28d8d"
  }
]


到目前为止,即使在没有收到赞的情况下,该代码也始终在任何单个帖子中返回true。

任何帮助,将不胜感激。

最佳答案

使用Array.prototype.some()

const clickLike = (userId, postId) => {
  return data.some(x => x._id === postId && x.likes.some(y => y._id === userId));
};




  const data = [
    {
      status: "published",
      _id: "5e5dcfc65104666e70558110",
      text: "Mierda!",
      user: {
        _id: "5e0925629648903308163aeb",
        username: "GodHimself",
        avatar:
          "//www.gravatar.com/avatar/b62ddb6758fca61fb7a56381ac7cb07d?s=200&r=pg&d=mm",
        bio: "I'm not God",
        id: "5e0925629648903308163aeb"
      },
      likes: [
        {
          _id: "5e76bc5fd3266e125c2dc4b9",
          user: "5e0925629648903308163aeb"
        }
      ],
      id: "5e5dcfc65104666e70558110"
    }
  ];
  const clickLike = (userId, postId) => {
    return data.some(x => x._id === postId && x.likes.some(y => y._id === userId));
  };
  console.log(
    clickLike("5e76bc5fd3266e125c2dc4b9", "5e5dcfc65104666e70558110"),
    clickLike("xxx", "5e5dcfc65104666e70558110"),
    clickLike("5e76bc5fd3266e125c2dc4b9", "xxx"),
    clickLike("xxx", "xxx")
  );

10-08 16:24