Question

I had a function that reads displays the contents of the SELECT query coming from WEBSQL of HTML5. I want to reuse that function but I have the problem since what am I getting is an Array of JSON object and I want to convert it it rows.item() so do anyone know how that works?

Example, I have this JSON Array

"retdic":[{"row_index":0,"lname":"Mato","age":26,"gender":"M","pic":"cyborg.jpg","fname":"Shibiru"},
          {"row_index":1,"lname":"Taro","age":30,"gender":"M","pic":"folder_wrench.png","fname":"Ichigo"},
          {"row_index":2,"lname":"Joni","age":27,"gender":"M","pic":"naruto.jpg","fname":"Perez"},
          {"row_index":3,"lname":"Sakura","age":24,"gender":"F","pic":"folder_table.png","fname":"Haruka"},              
          {"row_index":4,"lname":"Naruto","age":20,"gender":"M","pic":"naruto.jpg","fname":"Uzumaki"}]

How can I convert it to like $result.rows.item()? item() is not an array right coz if it is an array it should be item[].

UPDATE

Using the idea and help of Jeff I figured out how to do it. See the live example

Was it helpful?

Solution

Ok, so assuming that the only difference between your array and the item() function is that one is an array and the other a function (I'm assuming that you use item() as "$result.rows.item(3)" to get the 4th row), is pretty simple. Just define a function which takes a parameter i and returns the item at index i in the array. So:

var myItemFunction = function(i) {
     return retdic[i]
}

OTHER TIPS

you don't need to convert this object, you can access your data in this way:

$result.retdic[0].lname // 0 - row number

or in a loop :

var i,item;
for (i in $result.retdic) {
  item = $result.retdic[i];
  console.log(item.fname)
}

P.S.: if you wish, you can rename the 'retdic' key to 'rows'

$res = {"rows":[
  {"name": "a"},
  {"name": "b"},
  {"name": "c"}
]};


var someKey = 'name',
valueOfSomeKey = $res.rows[rowNumberHere][someKey];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top