Reputation: 526
well i am trying to assign values to my array in jquery its not working properly if i do it without for loop like i did for 0th element its working fine but if i put it in loop it goes undefined
var array1=<?php echo json_encode($array2)?>;
var array2=<?php echo json_encode($array1)?>;
var chartData = [
{
student: array1[0] ,
marks: array2[0]
}
];
var x=<?php echo json_encode($tquiz) ?>;
for (var i=1;i <= x ;i++ )
{
chartData[i]=[
{
student :array1[i],
marks:array2[i]
}
]
}
Upvotes: 3
Views: 107
Reputation: 10140
for (var i=1;i <= x ;i++ )
{
chartData[i]=[
{student :array1[i],
marks:array2[i]
}
]
}
change to
for (var i=1;i <= x ;i++ )
{
chartData.push({student :array1[i], marks:array2[i]});
}
By the way, i could refactor your finally code like this:
var array1=<?php echo json_encode($array2)?>;
var array2=<?php echo json_encode($array1)?>;
var x=<?php echo json_encode($tquiz) ?>;
var chartData = [];
for (var i=0;i <= x ;i++ ) {
chartData.push({student :array1[i], marks:array2[i]});
}
Upvotes: 1
Reputation: 15603
Use the foreach loop instead of for loop and here is problem in your for loop also:
It should be:
for (var i=1;i <= x.length ;i++ )
I recommend to you to use the foreach loop:
$.each( x, function( key, value ) {
Upvotes: 0