javascriptooprelational

How to build a relational structure in javascript?


I have 2 tables in DB:

1- Element:

2- Methods:

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 ?


Solution

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

    }

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