Here is my code:
I have two arrays[lab ,val] whose values are to be dynamicaly passed to object.
k is array to hold objects
var lab = ["1","2","3",4];
var val = ["sa","ma","pa","da"];
var k = [];
for(var i=0; i<4; i++) {
data[i] = {};
data[i].label = lab[i];
data[i].value = val[i];
k.push(data[i]) ;
}
document.write(k.data[0]);
document.write(" " + k.data[0] + "<br />");
Since you are already pushing the objects into an array, do it like this –
Your code is not working because you are indexing the values that
kstores, and yourdatavariable isn’t declared anywhere.You can even shorten the
forloop by directly pushing the objects into the array, which is a better way –In this way you won’t even need the temporary object.