根据条件从数组中删除重复元素

Remove duplicate elements from array based on condition

我有这个 reactions 数组,其中允许每个用户对每条消息只有一个反应:

const reactions = [
  {
    id: "c9c5a273-6033-4239-a6f4-09c067633680",
    profile: {
      first_name: "Pete",
      id: "1",
    },
    reaction_type: "love",
  },
  {
    id: "2e94ee98-7d84-49c5-ac69-600a76d90012",
    profile: {
      first_name: "Eve",
      id: "3",
    },
    reaction_type: "love",
  },
  {
    id: "2e94ee98-7d84-49c5-ac69-600a76d90013",
    profile: {
      first_name: "Brad",
      id: "4",
    },
    reaction_type: "thumbs_up",
  },
  {
    id: "30511fa3-3574-44f4-93f0-04ee35edc62b",
    profile: {
      first_name: "John",
      id: "2",
    },
    reaction_type: "lol",
  },
  {
    id: "30511fa3-3574-44f4-93f0-04ee35edc62b",
    profile: {
      first_name: "Bill",
      id: "5",
    },
    reaction_type: "thumbs_up",
  },
];

由于可能存在重复 reaction_type,我只想保留“我的”反应,例如。我是 id:4 的用户,然后删除所有其他反应的重复项。我不在乎 用户将从数组中删除。

现在我正在使用这个逻辑:


const uid = "4";

const uniqueReactions = () => {
  const myReactions = [];
  reactions.forEach((item) => {
    if (item.profile.id === uid) {
      myReactions.push(item);
    }
  });

  if (myReactions.length) {
    const uniques = reactions
      .filter((reaction) => {
        return (
          reaction.reaction_type !== myReactions[0].reaction_type &&
          reaction.profile.id !== uid
        );
      })
      .reduce(
        (r, i) =>
          !r.some(
            (j) => i.reaction_type === j.reaction_type && i.profile.id !== uid
          )
            ? [...r, i]
            : r,
        []
      );
    return [...myReactions, ...uniques];
  } else {
    return reactions.reduce(
      (r, i) =>
        !r.some((j) => i.reaction_type === j.reaction_type) ? [...r, i] : r,
      []
    );
  }
};

有没有更好的方法来处理这个问题?感觉又乱又吵。

编辑:为了清楚起见,我想保留“我的”反应,以便我可以在 UI.[=15= 中用不同的颜色突出显示它]

我认为最混乱的是大量使用三元和一个字母变量名。

也就是说,我认为它可以更简单。这是我的尝试:

function getUniqueReactions(id, reactions) {
    const userTypes = reactions
        .filter(reaction => reaction.profile.id === id)
        .map(reaction => reaction.reaction_type);
    const processedTypes = [];
    
    return reactions.reduce((accumulator, curItem) => {
        const { reaction_type: curType } = curItem; // The current type
        
        // If the current user has posted this type but this isn't the current user, skip the item
        if (userTypes.includes(curType) && curItem.profile.id !== id) {
            return accumulator;
        }
        
        // Check if either we're the supplied user and the type matches
        // or this is a reaction type the user hasn't posted and we haven't seen yet
        if (curItem.profile.id === id || !processedTypes.includes(curType)) {
            accumulator.push(curItem);
            processedTypes.push(curType);
        }
        
        return accumulator;
    }, []);
}

console.log(getUniqueReactions("4", reactions));