Return value from Object match

Posted by Hito_kun on Stack Overflow See other posts from Stack Overflow or by Hito_kun
Published on 2012-11-05T22:45:38Z Indexed on 2012/11/05 23:00 UTC
Read the original article Hit count: 282

Filed under:
|
|
|
|

I'm, by no means, JS fluent, so forgive me if im asking for some really basic stuff, but I've not being able to find a proper answer to my question.

Im writting my first Node.js (plus Extra Framework and Socket.io) app and Im having some fun setting up the server side of a FB-like messenger (surprise!!!).

So, let's say I have this data structure to store online users(This is a JSON Array, but I'm not sure it is the best way to do it or should I go with Javascript Objects):

[
  {
    "site": 45,
    "users": [
        {
            "idUser": 5,
            "idSocket": "qwe87r7w8qwe",
            "name": "Carlos Ray Norris"
        },
        {
            "idUser": 6,
            "idSocket": "v8d9d0fgfs7d",
            "name": "John Connor"
        }
    ]
},
{
    "site": 48,
    "users": [
          {
            "idUser": 22,
            "idSocket": "qwe87r7w8qwe",
            "name": "David Bowie"
          },
          {
            "idUser": 23,
            "idSocket": "v8d9d0fgfs7d",
            "name": "Barack H. Obama"
          }
      ]
  }
]

What I want to do is to search in the array for x value given y. In this case, retrieving the idSocket knowing the idUser WITHOUT having to run through the array values.

So I have basically 2 questions: first, what would be the proper way to store users online? and secondly, how to find values matching with the values I already know (find the idSocket that has a given idUser).

I would like a pure JS approach(or using some of the tools given by Node, Socket.io or Express), but if that's not possible then I can look for some JQuery.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about JSON