0

I have the follow json object. I am trying to filter through the data and get the socketId value where name is equal to bq89 ```name: "bq89"

const rooms = {
    "room1": {
        "socketId1":{
            id: "123",
            name: "the person name 1"
        },
        "socketId2":{
            id: "bq89",
            name: "the person name 2"
        }
    },
    "room2": {
        "socketId11":{
            id: "jkl",
            name: "room 2 name 1"
        },
        "socketId22":{
            id: "lpo",
            name: "room 2 name 2"
        }
    }
}

const socketId = rooms['room1'].filter(e=> {return e.name === 'bq89'})

console.log(socketId)
// desired output would be: socketId2
Jerry Seigle
  • 233
  • 4
  • 15
  • 2
    `filter` is for *arrays*, but there are no arrays here. Did you intend to use arrays instead of numbered object properties? (Which you should probably do.) – David Jan 12 '23 at 15:03
  • working with the data that is coming from the api. – Jerry Seigle Jan 12 '23 at 15:08
  • 3
    In that case if the API is returning poorly formatted data then you'll need to work around that. You might start [here](https://stackoverflow.com/q/8312459/328193) for example to iterate over object properties and construct a new object with only the properties you want. Overall what you have is a broken data structure, so you need to write custom code to perform standard operations on it. (Or write code to transform the structure into something more usable and then perform standard operations on that.) – David Jan 12 '23 at 15:11

1 Answers1

0

Using Object.entries to convert room1 to an array of entries allows you to use Array methods such as Array.find and Array.filter. You say you want to "filter the socketId value where name is equal to bq89" (singular) thus I assume by "filter" you actually mean "find".

const rooms = {
  "room1": {
    "socketId1": {
      id: "123",
      name: "the person name 1"
    },
    "socketId2": {
      id: "bq89",
      name: "bq89"
    }
  },
  "room2": {
    "socketId11": {
      id: "jkl",
      name: "room 2 name 1"
    },
    "socketId22": {
      id: "lpo",
      name: "room 2 name 2"
    }
  }
}

function getSocketId(name) {
  return Object.values(rooms)
    .flatMap(v => Object.entries(v))
    .find(([key, value]) => value.name === name)[0];
}

// desired output would be: socketId2
console.log(getSocketId("bq89"));

More generally, the data you're receiving from the API is broken and unconventionally formatted. If you have access to the API I suggest you change the way the data is structured.

Asplund
  • 2,254
  • 1
  • 8
  • 19
  • what happens if I want the socket with name `room 2 name 1`? – Diego D Jan 12 '23 at 15:33
  • Extract the logic into a function with room as a parameter. I can update my answer but it makes it easier if you specify the requirements in the question. – Asplund Jan 12 '23 at 16:01
  • when I read the question I expected all the rooms to be the subject of the search action.. did I get it wrong? anyway I didn't want to be pedantic but it just seemed the answer was not addressing the exact request. I'll give back the upvote but it was worth pointing out – Diego D Jan 12 '23 at 16:01
  • Sorry, I mistook you for the author, I don't believe the question specified that it should search the entire rooms object since it specifies `rooms['room1']` specifically. Although, of course it's more logical to search the entire object thus I updated my answer. @DiegoD – Asplund Jan 12 '23 at 16:08
  • yes you are right. The question explicitely uses `rooms['room1']`. My bad. I'll also update my answer that at this point has also very small reasons to remain there – Diego D Jan 12 '23 at 16:10
  • Just asking would this still be considered bad or broken api ````const newRoom = [ { room1: [ { socketId1: { id: 123, name: "Test device 1" } }, { socketId2: { id: 4, name: "Test device 2" } }, ], }, ]; ```` – Jerry Seigle Jan 12 '23 at 17:14
  • @JerrySeigle that's better but still not optimal, open a new question and I'd be happy to help. It's against SO policy to answer unrelated questions over comments – Asplund Jan 12 '23 at 17:38
  • @Undo Thank you. https://stackoverflow.com/questions/75101078/how-to-optimize-array-structure-with-objects-in-javascript-socket-io-project – Jerry Seigle Jan 12 '23 at 18:54