Reputation: 579
How do I loop through parameter [array], to create object properties?
As soon as I try to put for loop inside object declaration, it throws a lot of errors.
Is there any other way to do it?
Expected outcome is to loop through parameterArray and based on the length, create i amount of datasets.
function Constructor (parameterArray, fill, labels) {
this.parameterArray = parameterArray;
this.fill = fill;
this.labels = labels;
var object = {
name: {
label: labels
},
datasets: [
for (i=0; i<parameterArray.length; i++;) {
type: parameterArray[i]
fill: true;
};
]
}
};
var parameterArray = ["why", "u", "no", "work"];
var fill = true;
var labels = [1,2,3,4];
var helpme = new Constructor (parameterArray, true, labels);
Upvotes: 1
Views: 2783
Reputation: 5199
Use .map
to map each entry in the array to another one (I omitted parameter
as it seems undefined
) :
function Constructor (parameterArray, fill, labels) {
this.fill = fill;
this.labels = labels;
this.dataset = {
name: {
label: labels
},
datasets: parameterArray.map(type => ({
type, fill: true
}))
}
};
var parameterArray = ["why", "u", "no", "work"];
var fill = true;
var labels = [1,2,3,4];
var helpme = new Constructor (parameterArray, true, labels);
console.log('De nada', helpme)
Upvotes: 4