0

i have function with results parameter which consist of question_id and question as result from sql query i want to create array below as follow

quesArray['id']['1']['ques']=results['question'];

This is my code

var len = results.rows.length;
   console.log("DEMO table: " + len + " rows found.");
   var quesArray=new Array();
     for (var i=0; i<len; i++){
        console.log("Row = " + i + " ID = " + results.rows.item(i).del_ques_id + " Data =  " + results.rows.item(i).question);
        quesArray['id']['1']['ques']=results['question'];
     }
siddhesh
  • 563
  • 1
  • 9
  • 15
  • `quesArray['id']['1']['ques']` this is not the array in javascript. array's can be indexed only by int not by string so `quesArray['0][0][0]` is valid notation. – Jenish Rabadiya Feb 21 '15 at 10:11
  • This is like associative array. If you index something with a string value, it is considered as a property of an object. – Vaibhav Feb 21 '15 at 10:13
  • So your query results for a single question id or you get multiple question ids as well? If you elaborate then I can give you a solution. – Vaibhav Feb 21 '15 at 10:15

3 Answers3

0

You are trying to use associative arrays, which are "objects". Refer here

Community
  • 1
  • 1
Saki
  • 46
  • 4
  • welcome to StackOverflow. Please read http://stackoverflow.com/help/how-to-answer: "Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline." – Honza Zidek Feb 22 '15 at 09:29
0

There is no such thing as keyed arrays in JavaScript. But you can nest arrays:

(function () {
  
  "use strict";
  
    var threeDimArray = [
        [
            [1, 2, 3], [4, 777, 7]
        ],
        [
            [5, 4, 2], [4, 3, 5]
        ]
    ];
    // this will output 777 to the browser console
    console.log(threeDimArray[0][1][1]);

}());

So if you would like to keep arrays, convert your output to array structure, or iterate over array of objects (does your php return json ?)

NenadP
  • 585
  • 7
  • 24
0
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.

Mouser
  • 13,132
  • 3
  • 28
  • 54