A Wizard Did It
A Wizard Did It

Reputation: 3674

Creating an Object with unique ID's

What I'm looking to do is create an object with unique ID's. To better explain here's an example:

var obj = new Object({
    'a': {
        'someVariable': 'heh',
        'someOtherVariable': 'Stuff'
    },
    'c': {
        'someVariable': 'heh',
        'someOtherVariable': 'Stuff'
    }
});

Thing is, the object is going to be used/passed extensively, adding and removing items, but each item 'identifier' needs to must be unique... The object may hold 0 items at one point, or 700 at another. I felt the easiest way would be to have a number as the identifier for each object (instead of a or c) and enumerate over the Object.keys(obj), finding the highest number. Sadly, it doesn't appear you can use a number, as when I try to access obj.0, I get an error.

What I'd like the ID to be ideally is:

Anyone know of a way to do this?

Edit for elaboration

The project I'm doing this for is basically a workflow coordinator of sorts. The server side will create the object from various sources, including client side input, and create the object. This object is sent to the client which renders the information. Once completed by the client, the task is complete so it doesn't need to be displayed the next time the server updates the tasks list (by sending the object). The object is then completely removed from the Object, and the information is sent to a database for tracking purposes.

Upvotes: 4

Views: 944

Answers (3)

Peter Ajtai
Peter Ajtai

Reputation: 57685

You could use an array:

var obj = [];
obj.push( {'someVariable': 'heh','someOtherVariable': 'Stuff'});
obj.push( {'someVariable': 'heh2','someOtherVariable': 'Stuff2'});

// now obj.length == 2;
// access heh w/ obj[0]["someVariable"] or obj[0].someVariable 

or you could use numbered property names and a length property to keep track of how many there are for an "array-like" object:

var i = 0, obj ={}; 
obj.length = 0;

obj[i++] = {'someVariable': 'heh','someOtherVariable': 'Stuff'};
obj.length = i; 

obj[i++] = {'someVariable': 'heh2','someOtherVariable': 'Stuff2'};
obj.length = i; 

// now obj.length == 2
// access heh w/ obj[0]["someVariable"] or obj[0].someVariable 

Upvotes: 2

Thilo
Thilo

Reputation: 262534

obj.0 is a syntax error, because identifiers cannot start with a digit, but you can do obj[0] (which does the same thing):

 var obj = {};
 obj[0] = 'test';
 alert(obj[0]);

Upvotes: 3

Thilo
Thilo

Reputation: 262534

If the ids only need to be unique in a given page's DOM, you can just use a counter that you keep increasing.

However it seems like you need to have this id globally unique, across page reloads and across simultaneously accessing multiple browsers. In this case you need to generate the id server-side or use a UUID.

Or maybe I am misunderstanding things and you could just use an array? Push into the array to add new objects, and null them (not shifting elements around) when you want to delete them.

Upvotes: 2

Related Questions