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

JerryKur

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

cgatian

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I create objects from lists?

How do I create an object from array of objects using javascript?

How do I create an Array of Objects in C?

How do I merge an array of objects in Javascript?

how do I create an array of objects with array in the object from an object with the same key in javascript

How do I loop through Javascript array objects with forEach to create an element?

How do I create an array of partial objects from another array?

How do I declare the objects having array of objects in JavaScript?

How do I convert this php array into a JavaScript array of objects?

How do I filter an array object inside of a array of objects Javascript?

How do I create a react list component from an array of objects?

How do I create an array of objects the right way?

How do I create an boolean equals method compares objects in an array

How do I group by identifier and create an array of JSON objects in postgreSQL?

How do I create an array of inherited class objects?

How to create an array of objects in JavaScript?

how to create an array of objects in javascript

How can I create a constructor for an array of objects in JavaScript?

How do I create new arrays of objects with objects from an existing array, based on values in the objects?

How do I remove objects from a JavaScript associative array?

How do I list Javascript array of objects in HTML

Javascript - How do I map an array of objects to a datatable with column names?

How do I modify an object value in a cloned array of objects in Javascript

How do I convert array of Objects into one Object in JavaScript?

How do I convert an array of objects into an object (dynamically) in JavaScript

How do I reuse objects in an array for a particle system in JavaScript/jQuery?

How do I convert array of objects to object in javascript?

How do I check if a particular object exist or not in a JavaScript array of objects?

How do I return an array of objects with certain conditions in Javascript?