代码之家  ›  专栏  ›  技术社区  ›  Lelio Faieta

在带有map的数组中查找值,然后查找

  •  0
  • Lelio Faieta  · 技术社区  · 2 年前

    我有一个JSON对象,我想写一个函数来查找chat_userID并返回相应的nome。

    我想出了:

    utenti_chat = [{
        "nome": "John Doe",
        "id": 5,
        "email": "[email protected]",
        "chat_userID": "c1f2580cad95fa0f"
      },
      {
        "nome": "Jane Doe",
        "id": 6,
        "email": "[email protected]",
        "chat_userID": "ed86742608dde1fe"
      }
    ]
    
    
    userConnected = utenti_chat.filter(a => a.chat_userID.find(w => w == user.userID)).map(a => a.nome);

    但它回来了

    a.chat_userID.find is not a function
    

    我该怎么修?

    4 回复  |  直到 2 年前
        1
  •  3
  •   enzo    2 年前

    你不需要内在 find 因为 filter 已经为您进行了搜索:

    let expectedUserId = "ed86742608dde1fe"
    
    utenti_chat = [{
        "nome": "John Doe",
        "id": 5,
        "email": "[email protected]",
        "chat_userID": "c1f2580cad95fa0f"
      },
      {
        "nome": "Jane Doe",
        "id": 6,
        "email": "[email protected]",
        "chat_userID": "ed86742608dde1fe"
      }
    ]
    
    
    userConnected = utenti_chat.find(a => a.chat_userID == expectedUserId);
    if (userConnected == null) {
        console.log("Not found")
    } else {
        console.log(userConnected.nome)
    }
        2
  •  1
  •   Tal Rofe    2 年前

    首先,您使用 filter 方法,在这里没有那么大帮助。然后,你打电话 find 类型为的变量 string ,它没有调用的方法 发现 所以这就是你得到错误的原因。

    因此,如果有一些输入,比如 userId 得到你想要的东西的方法是,

    userId = "c1f2580cad95fa0f";
    
    utenti_chat = [{
        "nome": "John Doe",
        "id": 5,
        "email": "[email protected]",
        "chat_userID": "c1f2580cad95fa0f"
      },
      {
        "nome": "Jane Doe",
        "id": 6,
        "email": "[email protected]",
        "chat_userID": "ed86742608dde1fe"
      }
    ]
    
    
    userConnected = utenti_chat.find(a => a.chat_userID === userId)?.nome;
    
    console.log(userConnected);

    请记住,您应该处理边缘情况,主要是在找不到匹配用户的情况下 userId 。这就是为什么我使用 (...)?.nome 表示这个 ? 接线员代表处理我所描述的那个案子。当找不到匹配的用户对象时,的值 utenti_chat.find(a => a.chat_userID === userId) 将是 null 。在这种情况下,如果您不使用 ? 试图访问时会出现运行时错误的操作员 nome 的键 无效的

        3
  •  0
  •   Mike Warren    2 年前

    只需使用

    a => a.chat_userID == user.userID
    

    在您的 filter()

        4
  •  0
  •   roudlek    2 年前

    看起来你正试图根据chat_userID与特定值(user.userID)匹配的条件来过滤utenti_chat数组。然而,你的代码中有一个小错误。chat_userID属性是一个字符串,因此您不需要对其使用.find()。相反,您可以直接对其进行比较。

      const utenti_chat = [
    {
      "nome": "John Doe",
      "id": 5,
      "email": "[email protected]",
      "chat_userID": "c1f2580cad95fa0f"
    },
    {
      "nome": "Jane Doe",
      "id": 6,
      "email": "[email protected]",
      "chat_userID": "ed86742608dde1fe"
    }
      ];
      
      const userConnected = utenti_chat
        .filter(user => user.chat_userID === user.userID)
        .map(user => user.nome);
      
      console.log(userConnected);
    

    在此更正的代码中:

    我将.find(w=>w==user.userID)替换为===,以直接比较chat_userID和user.userID。 然后映射过滤后的数组以提取nome属性。 使用此函数之前,请确保已定义user.userID,以避免任何潜在问题。