Question

I have 2 tables in DB:

1- Element:

  • ElementID
  • ElementName

2- Methods:

  • MethodID
  • ElementID
  • MethodData

So there is a one to many relation between Elements and Methods,

I load these tables data using asp.net,

What i want to do is to send these data to my javascript, and javascript will do some functions on these data.

For example will loop through all elements and get each element methods and do some stuff with each method data.

I tried to make this as classes in jaascript but found my self wrote a lot of things,

First 2 classes, for elements and methods, then 2 arrays 1- for elements 2- for methods

And inside the methods array i wrote this:

this.findByElementId = function(elementId) {
    var result = [];
    for (var i = 0; i < this.methods.length; i++) {
        if (elementId === this.methods[i].methodElement) {
            result.push(this.methods[i]);
        }
    }
    return result;
}

which made my code slow.

My Question is how to represent this relational structure in javascript for more professional code and faster in processing ?

Was it helpful?

Solution 2

function Element() {
this.id;
this.type;
this.methods = new Array();

}

function Method() {
    this.id;
    this.methodType;
    this.methodData;
    this.methodElement;
    this.methodRank;
}

OTHER TIPS

I've never tried this stuff myself, but people have written LINQ-esque libraries for JavaScript, like JSINQ. (That page links to more JS LINQ libraries at the bottom of the page, too.)

It claims that you can "write arbitrarily complex queries against JavaScript arrays, DOM node lists or your own enumerable types", which sounds like what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top