Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
Search user in object and return key name if found

I'm new to Vue and Javascript and for my work I need to find the specific room certain user connected to. I'm still learning so i'm not very good with ES6 or javascript functions.

I need to search userId in rooms and return the name of the key ('pre', 'mid', 'post') if the id is found, if not then return null

const userId = "321b66bb-b44f-4859-994a-a339e604df62";

const rooms = {
    "pre": [
        {   
            "id": "321b66bb-b44f-4859-994a-a339e604df62", 
            "name": "George", 
            "age": "27", 
            "color": "#0B8043",
            "callIndex": 960433
        },
        {   
            "id": "1111111-11111-1111-1111-111111111111", 
            "name": "John", 
            "age": "30", 
            "color": "#0B8043",
            "callIndex": 4444444
        },
    ],
    "mid": [
        {   
            "id": "1111111-11111-1111-1111-111111111112", 
            "name": "John2", 
            "age": "30", 
            "color": "#0B8043",
            "callIndex": 4444444
        },
    ],
    "post": []
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can iterate through each key and then compare the id with passed userId and return then key name.

const userId = "321b66bb-b44f-4859-994a-a339e604df62",
      rooms = { "pre": [ { "id": "321b66bb-b44f-4859-994a-a339e604df62", "name": "George", "age": "27", "color": "#0B8043", "callIndex": 960433 }, { "id": "1111111-11111-1111-1111-111111111111", "name": "John", "age": "30", "color": "#0B8043", "callIndex": 4444444 }, ], "mid": [ { "id": "1111111-11111-1111-1111-111111111112", "name": "John2", "age": "30", "color": "#0B8043", "callIndex": 4444444 }, ], "post": [] },
      result = Object.keys(rooms).find(k => rooms[k].some(({id}) => id === userId)) || null;
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Easy task

const userId = "321b66bb-b44f-4859-994a-a339e604df62";

const rooms = {
  "pre": [{
      "id": "321b66bb-b44f-4859-994a-a339e604df62",
      "name": "George",
      "age": "27",
      "color": "#0B8043",
      "callIndex": 960433
    },
    {
      "id": "1111111-11111-1111-1111-111111111111",
      "name": "John",
      "age": "30",
      "color": "#0B8043",
      "callIndex": 4444444
    },
  ],
  "mid": [{
    "id": "1111111-11111-1111-1111-111111111112",
    "name": "John2",
    "age": "30",
    "color": "#0B8043",
    "callIndex": 4444444
  }, ],
  "post": []
};

function search(id) {
  for (let key in rooms) {
    // key is pre, mid or post
    for (let item of rooms[key]) {
      // item is an object containing id
      if (item.id === id) return key
    }
  }
  return null
}

console.log(search(userId))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda