[英]JavaScript (ReactJS) comparing two Objects
我有一個用戶對象:
const data = [
{
name: "John",
lastName: "Doe",
email: "stefa@gmail.com",
password: "123",
following: [{ id: "113"}, { id: "111" } }],
id: "112",
},
{
name: "Jane",
lastName: "Doe",
email: "dusica@gmail.com",
password: "123",
following: [{ id: "112" }],
id: "113",
},
{
name: "Mark",
lastName: "Twain",
email: "marko@gmail.com",
password: "123",
following: [],
id: "111",
},
];
如您所見,所有用戶都有一個名為“following”的數組,該數組包含用戶關注的用戶 ID。 我想訪問該數組“以下”以找出未關注哪些用戶。 假設我們要檢查 id="112" 的第一個用戶 John Doe 的“following”數組。
const followers = [];
let suggestions = null;
props.users.forEach((user) => {
if (user.id === '112') {
user.following.forEach((item) => {
followers.push(item);
});
}
});
followers.map((item) => {
suggestions = props.users.map((user) => {
if (user.id !== item.id && user.id !== '112) {
console.log(item.id);
return <div>something</div>;
}
});
});
我嘗試過這樣的事情,但結果不是我所期望的。 正如我所說,我想返回未被關注的用戶並呈現他們,因為我希望他們可見,以便用戶可以關注他們。 我希望我已經足夠理解了。 謝謝。
這是一個消極的比較。 所以你想過濾掉用戶關注的所有用戶。 您可以遍歷每個用戶並將其與以下數組進行比較。 如果以下數組包含用戶,則不要顯示它。
const notFollowing = allUsers.filter(user =>
!currentUser.following.some(({ id }) => id === user.id)
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.