0

I have the problem that i have an array like this :

var users = {
    'asidfjasd': {
       realname: 'John'  
    },
    'ggggggg': {
       realname: 'Peter'  
    }
}

And i want to access the users with

users[1]

to get

'ggggggg': {
       realname: 'Peter'  
}

Is there a way to do this with node.js?

EDIT

I have found a way to work around it by creating a second object with the usernames and them pointing to the id for the users-object. This might not work for other applications but it did it for mine.

CLOSED

10
  • 1
    That example code will give you an error. It should be an object, and you need to separate the properties with a comma. Or make it an array. Commented May 17, 2016 at 16:32
  • my bad with the comma but what do you mean with should be an object. I'm creating an array first and than a function creates a working object (the single users) and puts them into the array with a special key. Commented May 17, 2016 at 16:34
  • 1
    It's already an object (keys/values) but you're using array [] syntax. It will give you an error Commented May 17, 2016 at 16:35
  • 1
    You can't put objects into an array "with a special key". Arrays use number-based indexes. Commented May 17, 2016 at 16:36
  • Is there a possibility to get the objects in the parent object to be numeric so i can loop through them without changing the actual key? Commented May 17, 2016 at 16:39

1 Answer 1

2

You should look more closely at the structures you want to use.

Objects can be accessible using their keys. Keys are unique and are unordered. I think your changed example is correct (I added in an extra property for use in a later example).

So...

var users = {
    'asidfjasd': {
       realname: 'John'  
    },
    'ggggggg': {
       realname: 'Peter'  
    },
    'aaaa': {
       realname: 'Peter'
    }
}

users.ggggggg // { realname: "Peter" }
users['ggggggg'] // { realname: "Peter" }

Now, it is possible to iterate over this using Object.keys(users):

var keys = Object.keys(users);

To log names to the console you could do this:

keys.forEach(function (el) {
  console.log(users[el].realname);
});

To return an array of names you could do this:

var names = keys.map(function (el) {
  return users[el].realname;
});

How about turning your current data into an array you can access by numerical index:

var arrayOfObjs = keys.map(function (el) {
  return { socketname: el, realname: users[el].realname };
});

OUTPUT

[
  { "socketname": "asidfjasd", "realname": "John" },
  { "socketname": "ggggggg", "realname": "Peter" }
]

You can then use array methods to pull out the information you want from the objects. Say you want to get an array of objects where the realname is "Peter" assuming multiple "Peters" in the data:

var peters = arrayOfObjs.filter(function (el) {
  return el.realname === 'Peter';
});

OUTPUT

[
  { "socketname": "ggggggg", "realname": "Peter" },
  { "socketname": "aaaa", "realname": "Peter" }
]

DEMO CODE

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.