Creating an object with unique identifiers

What I want to do is create an object with unique identifiers. To better explain an example here:

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

The thing is, the object will be used / transmitted widely, adding and removing elements, but each element identifier must be unique ... An object can contain 0 elements at one point or 700 on another, I felt that the easiest way would be to have a number as an identifier for each object (instead of a or c ) and list by Object.keys(obj) , typing the largest number. Unfortunately, it does not appear, you can use a number, since when I try to access obj.0 , I get an error message.

I would like the ID to be ideally:

  • The number will be easiest to work with, although 100% is necessary if this is not possible.
  • When an object is deleted, the information it stores will be inserted into the database for tracking purposes, so it is vital that the identifier of the object remains unique, even if that identifier is no longer in the object. This is why I thought the numbers would be better since you can simply add +1 to each element.

Does anyone know how to do this?

Edit for development

The project that I am doing for this is basically a workflow coordinator. The server side will create an object from various sources, including client-side input, and create the object. This object is sent to the client, which displays the information. After completion by the client, the task is completed, so it does not need to be displayed the next time the task list server is updated (by sending an object). Then the object is completely removed from the object, and the information is sent to the database for tracking purposes.

+4
source share
3 answers

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

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

If identifiers should be unique only in a given DOM page, you can simply use a counter that you constantly increment.

However, it looks like you need to have this identifier globally unique, through page reloads and through simultaneous access to multiple browsers. In this case, you need to generate an id server or use the UUID .

Or maybe I misunderstand, and you can just use an array? Click in the array to add new objects, and zero them out (without moving the elements around) when you want to delete them.

+2
source

You can 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 can use the names of the numbered properties and the length property to keep track of how many exist for an array 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 
+2
source

All Articles