Im trying to create a class where the value accessed from a given array is dependend of a class parameter.
I have an array looking like this:
const wstv = [{
"shortName": "DMW",
"toExport": false,
"longName": "Dennis Manuel Walhter",
"date": "15.06.1978",
"value": 3
}, {
"shortName": "RTW",
"toExport": true,
"longName": "Ronald T. Wellov",
"date": "02.11.1966",
"value": 1,
}];
I want to create a class that finds the index of an array object and then outputs the information to the document.
const Bund = class Bund {
//expected input: (arrayName, valueOfArray[index].shortname to display
constructor(vrbnd, krzl) {
this.vrbnd = vrbnd;
this.krzl = krzl;
}
getElement(vrbnd, krzl){
const index = vrbnd.krzl.findIndex(shortName => shortName === this.krzl);
return index
}
//expected output: position of the given value of shortName in the given arrayName i.e. 0
draw() {
return this.toDraw();
}
toDraw() {
return function(){
document.write(vrbnd[getElement].longName + " " + vrbnd[getElement].date)
}
}
//ecpected output: given arrayName[index].longName i.e. "Dennis Manuel Walhter"
}
Then I want to create a class object var dmw = new Bund(wstv, dmw);
to then call dmw.draw();
. The whole purpose of this is to display information in a div. Since the final data set is made out of around 15 arrays with overall around 250 objects I need to be able to pass those arguments in to the class object.
Idealy I would afterwards be able to write something like:
for(let i = 0; i < givenArray.length; i++){
const givenArray[i].shortname = new Bund(givenArray, givenArray[i].shortName);
givenArray[i].shortname.draw();
}
to display all elements of an array.
Is it posssible to pass the arguments into the class to search the given Array for the given value?
Also, I am well aware that the getElement() "search function" does not work like this but I cant seem to find a solution to search the object position in the array of a prototype value inside the object. (I hope this sentence makes sense...)
Edit: There seems to be some confusion about the problem, so I will try to clarify.
I have multiple Arrays. I want to make a class that I can pass the following informations to:
Name of the array I want to access the data from; ([this.arrayName]
)
Value of one of the object arguments (from the object in the array that I am looking for) to get the rest of the needed information from that object; ([this.valueOfObjectArgument]
that would be a string from the object argument shortName
),
My class should be able to get and use the information needed from every class object.
The class itself should take that information ( [this.arrayName]
& [this.valueOfObjectArgument]
), pass it to a function (getElement()
) that gives me the position of [this.valueOfObjectArgument]
from [this.arrayName]
(.shortName
since I know where I am looking for that value) and returns the position in [this.arrayName]
. A second function then gets return value of getElement()
(an integer, the position of the object I was looking for in the array) and then displays all information of this object to a html element.
I hope this helps.