Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn value from Object match
    primarykey
    data
    text
    <p>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.</p> <p>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!!!).</p> <p>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):</p> <pre><code>[ { "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" } ] } ] </code></pre> <p>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 <strong>WITHOUT</strong> having to run through the array values.</p> <p>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).</p> <p>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.</p> <p><strong>EDIT</strong> I took chovy's advice and modified a little the structure, now it looks like this</p> <pre><code>{ 45: { // site 05: { // userId "name": "Carlos Ray Norris", "socketID": "qwe87r7w8qwe" }, 06: { "name": "John Connor", "socketID": "v8d9d0fgfs7d" } }, 48: { 22: { "name": "David Bowie", "socketID": "erv7e6v876vr" }, 23: { "name": "Barack H. Obama", "socketID": "brt877brtt87" } } } </code></pre> <p>Thanks to Node.js, I can easily access to the data like this (I know both, the site and userId value) <code>var socket = data[site]["05"].socketId</code>. Im guessing this is done thanks to Node.js, but Im not completely sure.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload