Vandervidi
Vandervidi

Reputation: 692

How to push to associative array in javascript?

I know this question was asked many times but i cant get it to work properly as i need. In a node.js ws i push objects into some array like this:

var arr = [];
arr.push({"name": "someName", "id": 12345});

and at the end i send it as a json in the response. How can i push into the array so ill be able to extract data like this ( in the client side ): ** the ID numbers are unique

var name = dataArr[12345];  //will return 'someName'

Im trying to avoid the need to iterate the whole array to get specific values.

Upvotes: 0

Views: 2419

Answers (1)

Quentin
Quentin

Reputation: 943193

If you want named properties, don't use an Array. Arrays are for ordered data structures accessed by index.

Use an Object instead.

var obj = {};
obj['12345'] = "someName";

Upvotes: 5

Related Questions