I need to create a dictionary of lists. Is that possible in Javascript? I am looking for something that will let me add objects for a feature/subfeature pairs and also iterate the feature/subfeature collections. My feature/subfeature data is a series of integer pairs:
[1,2], [1,3], [1,23], [2,4], [2, 12], ....
where the 1st number is the feature index, and the second number is the subfeature index. Each of these pairs can have a list of objects. I want to iterate the list by feature index and them by objects. Something like
forEach( item where feature index == someIndex, function(foo) {
forEach (item[someindex, foo.index] , function(bar) {
display bar.prop1, bar.prop2, ....
I will making a database call and add the results as items to this structure.
This structure is emulating something I put together in .Net using a dictionary that used a tuple as a key and list of objects as the value. The declaration was :
Dictionary <tuple[], list<myobject>>
Thanks,
Jerry
This example may be a bit more than you need. But perhaps you will find benefits.
//Object representing a Feature
function Feature(featureID, subFeatureID, list)
{
this.featureID = featureID;
this.subFeatureID = subFeatureID;
this.list = list;
}
//Object to hold features
function FeatureCollection()
{
this._collection = new Array();
}
//Augment the FeatureCollection prototype
FeatureCollection.prototype.add = function(feature)
{
this._collection.push(feature);
};
FeatureCollection.prototype.foreach = function(someIndex, listFunction)
{
//For each feature set, loop within the collection
//until the someIndex is found
for(var i=0,length=this._collection.length;i<length;i++)
{
//Store a local scoped variable of the feature
var feature = this._collection[i];
if(feature.featureID === someIndex)
{
//For each of the object within the feature's list
//invoke a function passing feature as 'this'
for(var x=0,xlength=feature.list.length; x<xlength;x++)
{
listFunction.call(feature, feature.list[x]);
}
break;
}
}
}
//Create a feature collection
var featureCollection = new FeatureCollection();
//Create a new feature
var testFeature = new Feature(1,2,["hello","world"])
//Add the feature to the collection
featureCollection.add(testFeature)
//Iterate the collection invoking the provided anonymous
//function if the index passed matches
featureCollection.foreach(1, function(listItem)
{
console.log("FeatureID: " + this.featureID + " ListItemValue:" + listItem)
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments