簡體   English   中英

檢查存儲在數組中的 object 中的用戶 ID 是否等於經過身份驗證的用戶的 ID

[英]Check if the User Id stored in an object in an array is equal to the Id of the Authenticated user

這是數組的結構:

post: [
   comments: {
        userId: 123123123,
        name: John doe
             }
      ]

我想檢查經過身份驗證的用戶的 ID (auth.user.id) 是否存在於存儲在 Post 數組中的任何評論中。 結果我想返回一個 boolean。

我試過使用 find() 方法和 includes() 方法但沒有成功。

posts.filter(comments => comments.userId === auth.user.id).length !== 0

如果帖子評論中存在用戶 ID,則此表達式的計算結果為真,否則計算結果為假

post: [
  comments: {
    userId: 123123123,
    name: John doe
  }
]

它無效。

應該是這個

 post : [{
   comments : {
     userId: 123123123,
     name: 'John doe'
   }
 }]

或這個

post: [
  {
     userId: 123123123,
     name: John doe
   }
]

 var post = [{ comments: { userId: 123123123, name: 'John doe' } }] console.log(post); var f = userId => post.some(x => x.comments.userId == userId); console.log(f(123123123))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM