1

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

2
  • Are the features and subfeature pairs unique? [1,2] will only occur once? I'm having a hard time relating the data model to some real world scenario. Could you elaborate? Commented Apr 15, 2014 at 23:24
  • Yes, they are unique pairs. It is a menu/submenu hierarchy for a report generator. The users will select at the feature level (ex. customer, internal, vendors, etc.) and in some cases a subfeature (ex for customer, internal or external ) sub menu will appear. For each of these feature/subfeature pair I need store and present a list of the latest reports they ran and that reports parameters for reselection. A sample entry stored might be customer | internal | [[type: monthly, start: 1/20/2005, end:2/15/2005, hyperlink : ], [type: monthly, start: 1/20/2012, end:3/15/2012, hyperlink : ]]. Commented Apr 16, 2014 at 0:48

3 Answers 3

1

A simple solution would be simply nested arrays, so something like

var arr = [[2,3]];

So each time you push to the array, you just add a new array as the entry

arr.push([1,2]);

Then I would keep a separate array to store the actual features/subfeatures and access them in directly using the number. So something like:

arr.forEach(function(item) {
    if (item[0] == someIndex) {
        subfeatures[item[1]].forEach(function(feature) {
            // Do something with the feature
        });
    }
});

Hope that puts you in the right direction!

Sign up to request clarification or add additional context in comments.

Comments

0

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)
});

http://jsbin.com/firiquni/1/edit

2 Comments

Thanks cgatian. I think this will work as the base for my Dictionary/Hash structures.
Glad you're going with something with some structure to it.
0

If you don't need anything fancy, and you know the limits of both arrays, there is a trick you can do.

Some might consider it hacky, others would consider it elegant.

Instead of using an array, you could use an object and hashes. Turn the two indexes into a string value to use as the hash key.

var myVals = {};
myVals["1,4"] = "Hi";
myVals["9,5"] = "There";

for (var i = 0; i < 10; i++) {
  for (j = 0; j < 10; j++) {
    var key = i + "," + j;
    var val = myVals[key];
    if (val) {
      // do something
    }
}

1 Comment

Anyone that thinks this is elegant doesn't know elegant code. While it does work, I wouldn't recommend it due to extensiblity issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.