views:

57

answers:

3

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:

  • A number would be easiest to work with, although is not 100% necessary if impossible.
  • When an object is removed the information it holds will be inserted into a database for tracking purposes, so it's vitally important that the object id remains unique even if that ID is not in the object any longer. This is why I thought numbers would be best, as you could just add +1 to each item.

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.

+1  A: 

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.

Thilo
Well it only needs to be unique within that Object, if that makes a difference.
Stanley
+1 for using a counter. It's easy to implement and totally sufficient to keep the id's unique.
David Winslow
Eh... `id` starting off with numbers is invalid, if you're planning to use them for HTML elements
Yi Jiang
@Yi Jiang: You can prefix the counter with a letter for element ids.
Thilo
@Yi Jiang - Doesn't sound like these IDs are referring to DOM element attributes.
Peter Ajtai
+2  A: 

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]);
Thilo
Awesome, awesome, awesome. This is great because it allows me to then increment the identifier. Is there an easier way to get the highest identifier rather than enumerating over every object manually?
Stanley
You could just use an array, I think. Then you can push new elements into it. Or keep the counter in a separate field like Peter Ajtai is suggesting in his answer.
Thilo
In removing an object then adding it again, wouldn't that cause duplicate ID's?
Stanley
Removing the last one would cause that, yes. You could keep a separate counter (also makes it easier to find the next value), or not remove the object, but set it to null.
Thilo
Also, if it is 'vitally important' that these ids are unique and not reused, maybe trusting client side code with it is not the way to go.
Thilo
The client doesn't manipulate the ID, just the data within each ID.
Stanley
+1  A: 

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 
Peter Ajtai