Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

91
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!