var quesArray=new Array();
for (var i=0; i<len; i++){
quesArray[results.rows[i].del_ques_id] = results.rows[i].question);
}
Based on the data you've provided this would be the correct approach.
It seems you're mixing PHP code into JavaScript item(i)
. JavaScript accesses normative arrays by index using [index]
.
So what's the solution: create an array index in the loop bases upon the id.
quesArray[results.rows[i].del_ques_id]
And attach the data to it:
results.rows[i].question)
This will result in an one dimensional array.
However you can expand
var quesArray=new Array();
for (var i=0; i<len; i++){
quesArray[results.rows[i].del_ques_id] = []; create a new array, dimension 2
quesArray[results.rows[i].del_ques_id].push(results.rows[i].question)); //add the question as a row.
}
This will create a two dimensional array where the id-indexes can contain multiple entries.
Please bear in mind that indexes can only be integers, so if your id
are string based this will still work, but the array store them as object properties and they are not enumerable by the default array way. You need a different approach then.