nested hashtables in javascript - javascript

I want to create the following data-structure with js:
folders dictionary: folder-id(guid) : ==> pages dictionary
pages array: page-position(int) : ==> files dictionary
files dictionary: file-id(guid) : ==> file object
I want to know at each time how many items are in each collection.
How would you suggest me to implemet this in JS ?
Should I use array or object with dynamically added properties?

Do the classes as follows.
function HashTable() {
var content = {};
var count = 0;
this.Add = function(key, value) {
if (content.hasOwnProperty(key)) throw new Error("Key already exist");
content[key] = value;
count++;
};
this.Set = function(key, value) {
if (!content.hasOwnProperty(key)) count++;
content[key] = value;
};
this.Get = function(key) {
if (!content.hasOwnProperty(key)) throw new Error("No such key");
return content[key];
};
this.AllKeys = function() {
var keys = [];
for (a in content) {
keys.push(a);
}
return keys;
};
this.Count = function() {
return count;
};
this.Remove = function(key) {
if (!content.hasOwnProperty(key)) throw new Error("No such key");
delete content[key];
count--;
};
}
// Then you can use it as follows
var folders = new HashTable();
folders.Add(1, 10);
alert(folders.Count());
alert(folders.Get(1));
folders.Remove(1);
alert(folders.Count());
It gives you a more rigid and OOP approach.
Edit
This ensures that your keys are unique, gives you count at any time and accepts integers and
strings as keys.

You can just write it out:
var folders = {
'folder1-guid': [
{'file1-guid': 'file1-content'},
{'file2-guid': 'file1-content'}
]
};
Alternatively, create Object and Array instances and assign the properties to them.

Related

Mixing Index Array with "Associative Array"

Since I need to access my items sometime by index and sometime by code. Is it a good idea to mix integer index with string index?
Note that the code, index, amount of items never changes after the data is loaded.
I'm thinking of doing something like this, where the same object is pushed and set as a hashtable.
function DataInformation(code, dataValue) {
this.code = code;
this.dataValue = dataValue;
}
var dataList = [];
function fillDataList() {
addNewData(new DataInformation("C1", 111));
addNewData(new DataInformation("C2", 222));
addNewData(new DataInformation("C3", 333));
}
function addNewData(newData) {
dataList.push(newData);
dataList[newData.code] = newData;
}
Then I would be able to access the object with either:
dataList[0].dataValue
dataList["C1"].dataValue
Before I used to loop to find the item.
function findItemByCode(code) {
for (var i = 0; i < dataList.length; i++) {
if (dataList[i].code == code) {
return dataList[i];
}
}
return null;
}
findItemByCode("C1").dataValue
Do you ever need to iterate dataList in strict order? Or is it just a bag of items for which you want random access by a certain key?
If ordered iteration is not a concern, use an object instead of an array. Watch out for key clashes, though.
var dataList = {};
function addNewData(newData) {
dataList[newData.code] = newData;
dataList[newData.dataValue] = newData;
}
// that's it, no other changes necessary
If key clashes can occur - or ordered iteration is necessary, or if you just want to make it particularly clean, use an array and an accompanying index object.
var dataList = [];
var dataIndex = {
byCode: {},
byValue: {}
};
function addNewData(newData) {
dataList.push(newData);
dataIndex.byCode[newData.code] = newData;
dataIndex.byValue[newData.dataValue] = newData;
}
Here is my try using Proxies
// Code goes here
function DataInformation(code, dataValue) {
this.code = code;
this.dataValue = dataValue;
}
var _dataList = [];
var dataList = new Proxy(_dataList, {
get: function(target, name) {
if (target && target.myMap && target.myMap[name]) return target[target.myMap[name]];
return target[name];
},
set: function(obj, prop, value) {
// The default behavior to store the value
obj.myMap = obj.myMap || {};
obj.myMap[value.code] = prop;
obj[prop] = value;
return true;
}
});
function fillDataList() {
addNewData(new DataInformation("C1", 111));
addNewData(new DataInformation("C2", 222));
addNewData(new DataInformation("C3", 333));
}
function addNewData(newData) {
dataList.push(newData);
}
fillDataList();
console.log(dataList[0].dataValue);
console.log(dataList["C1"].dataValue);

data structure - how to implement hash tables (including association lists) in javascript?

I am working on a hash table /data structure exercise but dont understand it quite well.
Each data has to be an instance of the list 'List' and using athe hash function, I need to add key /value pairs to the correct list and return the items based on their key.
As what I tried so far doesnt work, any help or explanation why what I have so far doesnt work would be much appreciated! Thank you!
function List () {
this.head=null;
}
function ListN (key, value, next) {
this.key = key;
this.value = value;
this.next = next;
}
List.prototype.set = function (key, value) {
var newNode=new ListN(key, value, this.head);
this.head=newNode;
};
List.prototype.get = function (key) {
var node = this.head;
while (node) {
if (node.key === key) {
return node.value;
}
node = node.next;
}
};
smallList = new List();
function HashT () {
this.data = Array(30);
}
HashT.prototype.set = function (key, value) {
var index=hash(key);
if (!this.data[index]) {
this.data[index]=new List();
}
this.data[index].set({key:key, value:value});
};
HashT.prototype.get = function (key) {
var index=hash(key);
return this.data[index];
};
The problem is simple, your mistake is here:
this.data[index].set({key:key, value:value});
It needs to be changed to
this.data[index].set(key, value);
In your HashT.prototype.get, the return statement needs to be:
return this.data[index].get(key);

Array that can store only one type of object?

Is it possible to create an array that will only allow objects of a certain to be stored in it? Is there a method that adds an element to the array I can override?
Yes you can, just override the push array of the array (let's say all you want to store are numbers than do the following:
var myArr = [];
myArr.push = function(){
for(var arg of arguments) {
if(arg.constructor == Number) Array.prototype.push.call(this, arg);
}
}
Simply change Number to whatever constructor you want to match. Also I would probably add and else statement or something, to throw an error if that's what you want.
UPDATE:
Using Object.observe (currently only available in chrome):
var myArr = [];
Array.observe(myArr, function(changes) {
for(var change of changes) {
if(change.type == "update") {
if(myArr[change.name].constructor !== Number) myArr.splice(change.name, 1);
} else if(change.type == 'splice') {
if(change.addedCount > 0) {
if(myArr[change.index].constructor !== Number) myArr.splice(change.index, 1);
}
}
}
});
Now in ES6 there are proxies which you should be able to do the following:
var myArr = new Proxy([], {
set(obj, prop, value) {
if(value.constructor !== Number) {
obj.splice(prop, 1);
}
//I belive thats it, there's probably more to it, yet because I don't use firefox or IE Technical preview I can't really tell you.
}
});
Not directly. But you can hide the array in a closure and only provide your custom API to access it:
var myArray = (function() {
var array = [];
return {
set: function(index, value) {
/* Check if value is allowed */
array[index] = value;
},
get: function(index) {
return array[index];
}
};
})();
Use it like
myArray.set(123, 'abc');
myArray.get(123); // 'abc' (assuming it was allowed)

Class-type object not returning values across module in node.js

I am writing a simple application in node, but having trouble with referencing objects from different modules. The object constructor and methods are (I skipped some methods to keep the excerpt short):
function Account (name, password) {
this._name = name;
this._password = password;
this._attributes = [];
}
Account.prototype.load = function (id) {
var self = this;
self = db.loadObject(id, 'account'); // separate module to save/retrieve data
this._name = self._name;
this._password = self._password;
this._attributes = self._attributes;
return this;
};
Account.prototype.getAttributes = function () {
return this._attributes;
}
Account.prototype.addAttributes = function (a) {
this._attributes.push(a);
};
module.exports = Account;
The db module is nothing fancy at this point:
var fs = require('fs');
var paths = {
'account' : './data/accounts/'
};
function loadObject (name, type) {
var filePath = paths[type] + name + '.json';
if (!fs.existsSync(filePath)) {
return false;
}
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
};
function saveObject (object, type) {
fs.writeFileSync(paths[type] + object.getName() + '.json', JSON.stringify(object),'utf8');
};
exports.loadObject = loadObject;
exports.saveObject = saveObject;
The file is saved as:
{"_name":"John","_password":"1234","_attributes":[["Jane","sub",[[10,1]]]]}
On my caller module I attempt to retrieve the attributes:
var Account = require('./account.js');
var account = new Account();
...
account.load(name);
...
var attr = account.getAttributes();
for (var item in attr) {
console.log(item[0]);
};
...
In the code above, the last loop prints undefined objects. I have checked the files and the information is saved and loaded without any problems. The array attr is not empty. If I print it with:
util.log(typeof attr+': '+attr);
I get:
object: Jane,sub,10,1
Instance issue? Should I rewrite the _attributes to be accessed directly via account.attributes?
This is the code you currently have for outputting your data:
var attr = account.getAttributes();
for (var item in attr) {
console.log(item[0]);
};
What this code does is output to the console the first character of each key in your _attributes field. With the data you've shown in your question, what this outputs is 0 because your _attributes field has this value: [["Jane","sub",[[10,1]]]]. When used in var item in attr the item variable will get only one value the string "0", and item[0] will also evaluate to the string "0". I've actually cut and pasted your code and data into files and ran your code to double check this and this is indeed what I get when I run the code. I don't get undefined values. A more sensible way to get the values out of the array would be:
var attr = account.getAttributes();
for (var item in attr) {
console.log(attr[item]);
}
If you want to iterate two levels deep:
for (var item in attr) {
var value = attr[item];
for (var item2 in value) {
console.log(value[item2]);
}
}

Recursive nested property creation in JavaScript

I am trying to recursively build an object with a tree of properties based on a MongoDB-ish selector "top.middle.bottom". There are some underscorejs helpers as well:
function setNestedPropertyValue(obj, fields, val) {
if (fields.indexOf(".") === -1) {
// On last property, set the value
obj[fields] = val;
return obj; // Recurse back up
} else {
var oneLevelLess = _.first(fields.split("."));
var remainingLevels = _.rest(fields.split(".")).join(".");
// There are more property levels remaining, set a sub with a recursive call
obj[oneLevelLess] = setNestedPropertyValue( {}, remainingLevels, val);
}
}
setNestedPropertyValue({}, "grandpaprop.papaprop.babyprop", 1);
Desired:
{
grandpaprop: {
papaprop: {
babyprop: 1
}
}
}
Outcome:
undefined
Helps and hints would be appreciated.
Instead of recursion I would choose for an iterative solution:
function setNestedPropertyValue(obj, fields, val)
{
fields = fields.split('.');
var cur = obj,
last = fields.pop();
fields.forEach(function(field) {
cur[field] = {};
cur = cur[field];
});
cur[last] = val;
return obj;
}
setNestedPropertyValue({}, "grandpaprop.papaprop.babyprop", 1);
EDIT
And here is another version thanks to the suggestions by Scott Sauyet:
function setPath(obj, [first, ...rest], val) {
if (rest.length == 0) {
return {...obj, [first]: val}
}
let nestedObj = obj[first] || {};
return {...obj, [first]: setPath(nestedObj, rest, val)};
}
function setNestedPropertyValue(obj, field, val) {
return setPath(obj, field.split('.'), val);
}
// example
let test_obj = {};
test_obj = setNestedPropertyValue(test_obj, "foo.bar.baz", 1);
test_obj = setNestedPropertyValue(test_obj, "foo.bar.baz1", 1);
// will output {"foo":{"bar":{"baz":1,"baz1":1}}}, while in the original version only "baz1" will be set
console.log(JSON.stringify(test_obj));
It's plain javascript
It only appends properties and will not override a top level object
setNestedPropertyValue() does not mutate the passed object (although keep in mind it only returns a shallow copy of the object, so some properties may be shared references between the original object and the new one)
I know this is old, but I needed exactly that kind of function and wasn't happy with the implementation, so here is my version:
function setNestedPropertyValue(obj, field, val) {
if (field.indexOf(".") === -1) {
obj[field] = val;
} else {
let fields = field.split(".");
let topLevelField = fields.shift();
let remainingFields = fields.join(".");
if (obj[topLevelField] == null) {
obj[topLevelField] = {};
}
setNestedPropertyValue(obj[topLevelField], remainingFields, val);
}
}
// example
let test_obj = {};
setNestedPropertyValue(test_obj, "foo.bar.baz", 1);
setNestedPropertyValue(test_obj, "foo.bar.baz1", 1);
// will output {"foo":{"bar":{"baz":1,"baz1":1}}}, while in the original version only "baz1" will be set
console.log(JSON.stringify(test_obj));
It's plain javascript
It only appends properties and will not override a top level object
setNestedPropertyValue() does not return the object so it is clear that it mutates the passed object
As mentioned by Jack in the question, I was not returning my object in the last line in the else statement. By adding this, it is now working:
obj[oneLevelLess] = setNestedPropertyValue( {}, remainingLevels, val);
return obj; // Add this line
}

Categories