dimanche 10 avril 2016

Maintaining a key index steady

I have an object used as index.

const index= {};

I use a counter to get a new key:

var key=0; 
function getNewKey(){
   return ++key;
}

Then I can add object to index:

function addObject(object){
    const key= getNewKey();
    index[key]= object;
    return key;
}

But, like that, if I remove objects in index, and add new ones, there will be holes, and the key may become big.

So I would like to know if there was patterns for this kind of problem, which often appears.

Aucun commentaire:

Enregistrer un commentaire