Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda