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

90
Vistas
How to filter Object from Object

I have 2 or more objects in Main Object. How can I find one object againt this key value.

{
    "0": {
        "component": "AWAY",
        "currentUser": {
            "id": 1,
            "userName": "abc",
            "inRoom": false,
            "image": ""
        },
    },
    "1": {
        "component": "PHONE_BOTH",
        "currentUser": {
            "id": 1,
            "userName": "abc",
            "inRoom": false,
            "image": ""
        }
    },
    "2": {
        "component": "MEETING_ROOM",
        "currentUser": {
            "id": 1,
            "userName": "abc",
            "inRoom": true,
            "image": ""
        }
    }
}

I just want to get one object where inRoom = true

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

It is not recommended to index an object with numeric keys. Use an array instead then you can use filter directly on the array

const arr = [{
    "component": "AWAY",
    "currentUser": {
      "id": 1,
      "userName": "abc",
      "inRoom": false,
      "image": ""
    },
  },
  {
    "component": "PHONE_BOTH",
    "currentUser": {
      "id": 1,
      "userName": "abc",
      "inRoom": false,
      "image": ""
    }
  },
  {
    "component": "MEETING_ROOM",
    "currentUser": {
      "id": 1,
      "userName": "abc",
      "inRoom": true,
      "image": ""
    }
  }
]
console.log(arr.filter(({currentUser}) => currentUser.inRoom === true))

about 4 years ago · Santiago Trujillo Denunciar

0

You should convert your dictionary to an array and then find the object by a specific criteria using the find() array method. See implementation:

// usersDict is the given Main Object

// convert usersDict to array of user objects
let usersArray = Object.values(usersDict);

// find user which has inRoom flag true
let userInRoom = usersArray.find(o => !!o.currentUser.inRoom);
about 4 years ago · Santiago Trujillo Denunciar

0

    var bb = {
        "0": {
            "component": "AWAY",
            "currentUser": {
                "id": 1,
                "userName": "abc",
                "inRoom": false,
                "image": ""
            },
        },
        "1": {
            "component": "PHONE_BOTH",
            "currentUser": {
                "id": 1,
                "userName": "abc",
                "inRoom": false,
                "image": ""
            }
        },
        "2": {
            "component": "MEETING_ROOM",
            "currentUser": {
                "id": 1,
                "userName": "abc",
                "inRoom": true,
                "image": ""
            }
        }
    }
    
    var kk = []
    for (const man in bb) {
      if(bb[man].currentUser.inRoom){
          kk.push({man: bb[man]})
      }
    }

console.log(kk.length ? kk[0] : {})
about 4 years ago · Santiago Trujillo 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