JerryKur
JerryKur

Reputation: 7519

How do I create a javascript array of lists of objects?

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

Upvotes: 1

Views: 5370

Answers (3)

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

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

Upvotes: 0

cgatian
cgatian

Reputation: 22984

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

Upvotes: 0

trentmwillis
trentmwillis

Reputation: 671

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!

Upvotes: 1

Related Questions