0

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.

  • Possible duplicate of https://stackoverflow.com/questions/13964155/get-javascript-object-from-array-of-objects-by-value-of-property – 04FS May 15 '19 at 10:45
  • Mentioned dupe is for the finding object in array based on specific property value … part. As for the rest of your question, not sure what you are on about. – 04FS May 15 '19 at 10:46
  • `Is it possible to find the index of an object argument in an array?` Yes. – Vencovsky May 15 '19 at 10:51

2 Answers2

0

I think your problem is overengineering. Using a for loop would help you with getting the index easily. Then you just compare the keys/values and return the one you need. Tell me if this helps

function searchForShortInArray(shortName) {
  for (let i = 0; i < wstv.length; i++) {
    if (wstv[i].shortName == shortName) {
      return {index: i, entry: wstv[i]}
    }
  }
}

console.log(searchForShortInArray("RTW"));

Output:

{ index: 1,
  entry:
   { shortName: 'RTW',
     toExport: true,
     longName: 'Ronald T. Wellov',
     date: '02.11.1966',
     value: 1 } }
Martin Goncharov
  • 130
  • 2
  • 10
0

You can use the IndexOf() method to check the position or index of that particular value, in that array , and then console log array[index]