patternjavascriptMinor
Object map in JavaScript
Viewed 0 times
javascriptobjectmap
Problem
I want to make a map object that key is object.
But, JavaScript only allows string as a hash key. (at least before ECMAScript6 comes?)
So, I tried to write auxiliary functions to emulate, hash-ish (but actually linear search).
Are there any suggestions on improvement?
But, JavaScript only allows string as a hash key. (at least before ECMAScript6 comes?)
So, I tried to write auxiliary functions to emulate, hash-ish (but actually linear search).
Are there any suggestions on improvement?
var tabInfo = {};
var tabInfoKey = 0;
function getTabInfoItem(tab) {
console.log("getTabInfoItem", tab);
for(var key in tabInfo) {
if(tabInfo[key].tab === tab) {
return tabInfo[key].info;
}
}
}
function setTabInfoItem(tab, info) {
console.log("setTabInfoItem", tab, info);
for(var key in tabInfo) {
if(tabInfo[key].tab === tab) {
tabInfo[key] = {
tab: tab,
info: info
};
return;
}
}
tabInfoKey++;
tabInfo[tabInfoKey] = {
tab: tab,
info: info
};
}
function deleteTabInfoItem(tab) {
console.log("deleteInfoItem", tab);
for(var key in tabInfo) {
if(tabInfo[key].tab === tab) {
delete tabInfo[key];
return;
}
}
}Solution
I can't see a good use for this, but nonetheless:
Something like this incorporates my feedback:
You can then
Furthermore you should consider whether you want to allow function chaining, right now none of these functions return anything and it seems a shame.
- Since you treat tabInfo as a list, why not have it be an array, this way you don't need
tabInfoKeyand you can usepushinstead. Arrays are objects too.
- Don't use console.log for production code
tabis a terrible name, go for either the Spartanoorobject
infois an unfortunate name, perhapsvalue?
tabInfois also meh, perhapsobjectMap?
- you should have a constructor for this!
Something like this incorporates my feedback:
function ObjectMap() {
this.entries = [];
}
ObjectMap.prototype.get = function( object ) {
for( var i = 0 , length = this.entries.length ; i < length ; i++ ){
if( this.entries[i].object === object ){
return this.entries[i].value;
}
}
}
ObjectMap.prototype.set = function( object, value ) {
//If we can find it, update it
for( var i = 0 , length = this.entries.length ; i < length ; i++ ){
if( this.entries[i].object === object ){
this.entries[i].value = value;
return;
}
}
//Otherwise, generate an entry
this.entries.push( { object : object , value : value } );
}
ObjectMap.prototype.remove = function( object ) {
for( var i = 0 , length = this.entries.length ; i < length ; i++ ){
if( this.entries[i].object === object ){
this.entries.splice( i , 1 );
return;
}
}
}You can then
o = new ObjectMap();
o.set( o , "123" );
o.get( o ); //Gives "123"
o.remove( o );
o.get( o ); //Gives nothingFurthermore you should consider whether you want to allow function chaining, right now none of these functions return anything and it seems a shame.
Code Snippets
function ObjectMap() {
this.entries = [];
}
ObjectMap.prototype.get = function( object ) {
for( var i = 0 , length = this.entries.length ; i < length ; i++ ){
if( this.entries[i].object === object ){
return this.entries[i].value;
}
}
}
ObjectMap.prototype.set = function( object, value ) {
//If we can find it, update it
for( var i = 0 , length = this.entries.length ; i < length ; i++ ){
if( this.entries[i].object === object ){
this.entries[i].value = value;
return;
}
}
//Otherwise, generate an entry
this.entries.push( { object : object , value : value } );
}
ObjectMap.prototype.remove = function( object ) {
for( var i = 0 , length = this.entries.length ; i < length ; i++ ){
if( this.entries[i].object === object ){
this.entries.splice( i , 1 );
return;
}
}
}o = new ObjectMap();
o.set( o , "123" );
o.get( o ); //Gives "123"
o.remove( o );
o.get( o ); //Gives nothingContext
StackExchange Code Review Q#25868, answer score: 5
Revisions (0)
No revisions yet.