skip to Main Content

I have a JSON object and I want to write a function to look for the chat_userID and return the corresponding nome.

I came up with:

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);

but it returns that

a.chat_userID.find is not a function

how can I fix it?

4

Answers


  1. You don’t need the inner find, because filter already does the searching for you:

    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)
    }
    Login or Signup to reply.
  2. Just use

    a => a.chat_userID == user.userID
    

    in your filter()

    Login or Signup to reply.
  3. First, you use the filter method which won’t be that helpful here. Then, you call find on a variable which its type is an string, which has no method called find so that is why you get the error.

    So given some input, say userId the way to get what you want would be,

    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);

    Keep in mind you should handle edge cases, mostly where could not find a matching user with the userId. This is why I used the (...)?.nome expression. The ? operator stands for handling that case I described. When a matching user object won’t be found, the value of utenti_chat.find(a => a.chat_userID === userId) would be null. In that case if you won’t user the ? operator you will get run time error for trying to access nome key of a null.

    Login or Signup to reply.
  4. It looks like you’re trying to filter the utenti_chat array based on the condition that the chat_userID matches a specific value (user.userID). However, there’s a small mistake in your code. The chat_userID property is a string, so you don’t need to use .find() on it. Instead, you can directly compare it.

      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);
    

    In this corrected code:

    I replaced .find(w => w == user.userID) with === to directly compare chat_userID with user.userID.
    The filtered array is then mapped to extract the nome property.
    Make sure that user.userID is defined before using this function to avoid any potential issues.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search