【问题标题】:Filter an array to return matching id's of a nested array in JavaScript [duplicate]过滤数组以返回 JavaScript 中嵌套数组的匹配 ID [重复]
【发布时间】:2022-01-29 03:30:28
【问题描述】:

例如,我有一个 location_id 为 1 的客户端对象。

还有一群员工。每个工作人员都包含一个嵌套的 location_ids 数组。我想过滤人员数组以将在其 located_at 数组中具有匹配 id 条目的所有人员返回到与客户端相同的 location_id

staffMember obj 看起来是这样的:

contact_number: "4168455302"
email: "benshekhtman@hotmail.com"
first_name: "Ben"
id: 2
last_name: "Shekhtman"
located_at: [
  {id: 1, name: "Ben's Location"}
  {id: 2, name: 'My second location'}
  {id: 7, name: 'Medicare Clinic'}
]

这是我尝试的逻辑:

  const filterStaffByLocation = staff.filter((s) => {
    s.located_at.some((location) => location.id === client.location_id)
  })

当前 filterStaffByLocation 返回一个空的 []

更多详情: 如果人员数组如下所示:

[{
contact_number: "4168455302"
email: "benshekhtman@hotmail.com"
first_name: "Ben"
id: 2
last_name: "Shekhtman"
located_at: [
  {id: 1, name: "Ben's Location"}
  {id: 2, name: 'My second location'}
  {id: 7, name: 'Medicare Clinic'}
]
provider_id: 2
user_id: 2
user_type: "ADMIN"
},
{
contact_number: "9053700017"
email: "test@email.com"
first_name: "Test "
id: 3
last_name: "Team Member"
located_at: [
 {id: 3, name: 's'}
 {id: 7, name: 'Medicare Clinic'}
]
provider_id: 2
user_id: 7
user_type: "SUPPORT"
}]

针对 location_id 为 1 的客户端进行测试应该只返回第一个条目(id 为 2 的人员成员)

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:

    在你的队伍中

    s.located_at.some((location) => location.id === client.location_id)
    

    您忘记返回结果。

    改成

    return s.located_at.some((location) => location.id === client.location_id)
    

    const client = {
        location_id:7
    }
    
    const staff = [{
    contact_number: "4168455302",
    email: "benshekhtman@hotmail.com",
    first_name: "Ben",
    id: 2,
    last_name: "Shekhtman",
    located_at: [
      {id: 1, name: "Ben's Location"},
      {id: 2, name: 'My second location'},
      {id: 7, name: 'Medicare Clinic'},
    ],
    provider_id: 2,
    user_id: 2,
    user_type: "ADMIN",
    },
    {
    contact_number: "9053700017",
    email: "test@email.com",
    first_name: "Test ",
    id: 3,
    last_name: "Team Member",
    located_at: [
     {id: 3, name: 's'},
     {id: 7, name: 'Medicare Clinic'},
    ],
    provider_id: 2,
    user_id: 7,
    user_type: "SUPPORT",
    }]
    
      const filterStaffByLocation = staff.filter((s) => {
        return s.located_at.some((location) => location.id === client.location_id)
      })
      
      console.log(filterStaffByLocation)
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案2】:

      希望我明白你想要什么。但下面是如何做到这一点

      
      /*
         Return all staff that have the location_id passed in
      */
      const staffAtUserId = (staffArray,id)=>{
         // List to return 
         const retArr = []
         // Loop through all staff
         staffArray.forEach(staff=>{
             // Check if staff has that id in there location
             const hasLocationId = staff.located_at.some(obj=>obj.id===id)
             // If true at to return list
             if (hasLocationId){
                 retArr.push(staff)
             }
         })
         return retArr
      }
      
      const staffArray = [{
          contact_number: "4168455302",
          email: "benshekhtman@hotmail.com",
          first_name: "Ben",
          id: 2,
          last_name: "Shekhtman",
          located_at: [
            {id: 1, name: "Ben's Location"},
            {id: 2, name: 'My second location'},
            {id: 7, name: 'Medicare Clinic'},
          ],
          provider_id: 2,
          user_id: 2,
          user_type: "ADMIN",
          },
          {
          contact_number: "9053700017",
          email: "test@email.com",
          first_name: "Test ",
          id: 3,
          last_name: "Team Member",
          located_at: [
           {id: 3, name: 's'},
           {id: 7, name: 'Medicare Clinic'}
          ],
          provider_id: 2,
          user_id: 7,
          user_type: "SUPPORT",
      }]
      console.log(staffAtUserId(staffArray,7))
      

      【讨论】:

        猜你喜欢
        • 2021-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-17
        • 2022-01-25
        • 2022-01-26
        • 2017-11-12
        • 1970-01-01
        相关资源
        最近更新 更多