1

I'm trying to make an array that has data stored in itself, and then I'd like to request the data by just calling an users their id myarray[userId].language.

This is the code I'm using but how can I make it so myId actually becomes someones id and not just a string?

var myId = 62;
var users = [];

users.push({myId:{ //myId should be 62
    language:"Dutch",
    born:1999
}});

JSFiddle

Martijn Ebbens
  • 253
  • 3
  • 15
  • what do you want to do ? this code is not similar to the thing that you want ! – Mohsen_Fatemi Sep 02 '17 at 15:06
  • 5
    Don't use push. `users[myId] = { language:"Dutch", born:1999 };` – mplungjan Sep 02 '17 at 15:08
  • I want to push my data that when I request the id from the users array like so: `users[62]` that it will return the array `{language: "Dutch", born: 1999}` – Martijn Ebbens Sep 02 '17 at 15:08
  • @mplungjan This doesn't look like a typo, and the problem is definitely reproducible. – 4castle Sep 02 '17 at 15:10
  • @4castle - _caused by a problem that can no longer be reproduced_ – mplungjan Sep 02 '17 at 15:10
  • @mplungjan is there any difference? I've always used `push` till this point. – Martijn Ebbens Sep 02 '17 at 15:11
  • Then you would have `users[0] = {}; users[0][myId]={ //myId should be 62 language:"Dutch", born:1999 }` which would not allow you to do users[62] – mplungjan Sep 02 '17 at 15:13
  • @mplungjan Please don't use that close reason for questions where the problem is reproducible. Just because your comment shows how to fix the problem, it doesn't mean the question should be closed. – 4castle Sep 02 '17 at 15:13
  • @4castle - I believe that is up to my discretion to use that - using push on an object is not working. There are no other close reasons for something simple that does not work because of wrong assumption – mplungjan Sep 02 '17 at 15:15
  • @mplungjan There are no other close reasons because it's an invalid close reason. Do not close questions just because they are obvious to you. The OP had no idea how to fix their problem, so it is a valid question. – 4castle Sep 02 '17 at 15:17
  • @MartijnEbbens if you push 62 other users you will have what you want: `var users = []; users.push({ language:"Dutch", born:1999 });` this will be user 0 – mplungjan Sep 02 '17 at 15:17
  • @4castle - I disagree. It was closed as a dupe anyway. I could have hammerclosed it as a dupe to had I looked – mplungjan Sep 02 '17 at 15:18
  • @mplungjan Thanks, it works, I knew this question should be arround somewhere else but I really couldn't find the right termology to find the question and didn't really knew what to look for. – Martijn Ebbens Sep 02 '17 at 15:19
  • You normally don't and you cannot – mplungjan Sep 02 '17 at 15:25

2 Answers2

0

You could use an object instead of an array and take the id as key for the object.

var myId = 62,
    users = {};

users[myId] = {
    language: "Dutch",
    born: 1999
};

console.log(users);
console.log(users[myId].language);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can now evaluate expressions in object literal property names by using square brackets.

var myId = 62;
var users = [];

users.push({
  [myId]: { //myId is 62
    language: "Dutch",
    born: 1999
  }
});

console.log(JSON.stringify(users, null, 2));

Use a transpiler if you need to support legacy engines.


Of course you can do this without .push() too.

var myId = 62;
var users = [{
  [myId]: { //myId is 62
    language: "Dutch",
    born: 1999
  }
}];

console.log(JSON.stringify(users, null, 2));
spanky
  • 2,768
  • 8
  • 9