Javascript:$result.rows.item(x) 变量类型是什么?
我有一个函数可以读取显示来自 HTML5 WEBSQL 的 SELECT 查询的内容。我想重用该函数,但我遇到了问题,因为我得到的是一个 JSON 对象数组,并且我想将其转换为 rows.item() 那么有人知道它是如何工作的吗?
例如,我有这个 JSON 数组,
"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"}]
如何将其转换为 $result.rows.item() 之类的? item() 不是一个数组,因为如果它是一个数组,它应该是 item[]。
更新
利用杰夫的想法和帮助,我想出了如何做到这一点。请参阅实时示例
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,假设您的数组和 item() 函数之间的唯一区别是一个是数组,另一个是函数(我假设您将 item() 用作“$result.rows.item(3)” “获得第四行),非常简单。
只需定义一个函数,它接受参数 i 并返回数组中索引 i 处的项目。
所以:
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:
你不需要转换这个对象,你可以通过这种方式访问你的数据:
或者在循环中:
PS:如果你愿意,你可以将“retdic”键重命名为“rows”
you don't need to convert this object, you can access your data in this way:
or in a loop :
P.S.: if you wish, you can rename the 'retdic' key to 'rows'