I am trying to create a script to automatically read data from a file and generate objects to generate HTML for each object then. Each object represents multiple data sequences, but this doesn't really matter here.
Since I haven't had much experience with javascript classes, I'm somewhat struggling to grasp their concept compared to other programming languages.
Say I have the following class:
class DataSet{
title = String
description = String
attributes = []
constructor(title, description) {
this.title = title
this.description = description
}
setAttribute(a){this.attributes.push(a)}
}
From the data, I'm then creating an array of DataSet objects dataSets
. Because of the input data's nature, I need to update the DataSet elements of my array iteratively.
Now, this is where I struggle because I cannot call functions, like setAttribute
, as javascript doesn't know the type of an object within my array when iterating over it.
In case it's of interest, I'm simply doing a foreach loop:
for(let data in dataSets) {
data.setAttribute(a)
}
A quick test for typeof dataSets.splice(-1)
call returns object
(understandably). Simply calling the function like dataSets[i].setAttribute('attribute')
returns an Uncaught TypeError: dataSets[i].setAttribute is not a function
.
But how can I manipulate the DataSet that is this object, and I cannot find anything similar here or mdn_web_docs?
Maybe I am completely off here, so I appreciate any help :)