'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



Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

    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] : {})

Solution 4:[4]

First, you should convert your object to an array then use the filter method in the array:

// obj = your data
Object.values(obj).filter(obj => obj.currentUser.inRoom)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mplungjan
Solution 2 BrunoT
Solution 3 Pratik Ranpariya
Solution 4 Mohammad Hooshdar