3

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]
                    }
                 ]    
}
0

2 Answers 2

1
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]});
}
Sign up to request clarification or add additional context in comments.

2 Comments

ah thanx,i will never understand why every programing language come up with a new method for everything
There are two siple methods that help your working with arrays in JS (push & pop)
0

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 ) {

1 Comment

umm no tquiz is a single value variable,loop is running the number of times it should run

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.