Javascript:$result.rows.item(x) 变量类型是什么?

发布于 2024-12-18 15:12:05 字数 909 浏览 2 评论 0原文

我有一个函数可以读取显示来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请远离我 2024-12-25 15:12:05

好的,假设您的数组和 item() 函数之间的唯一区别是一个是数组,另一个是函数(我假设您将 item() 用作“$result.rows.item(3)” “获得第四行),非常简单。
只需定义一个函数,它接受参数 i 并返回数组中索引 i 处的项目。
所以:

var myItemFunction = function(i) {
     return retdic[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:

var myItemFunction = function(i) {
     return retdic[i]
}
木森分化 2024-12-25 15:12:05

你不需要转换这个对象,你可以通过这种方式访问​​你的数据:

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

或者在循环中:

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

PS:如果你愿意,你可以将“retdic”键重命名为“rows”

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


var someKey = 'name',
valueOfSomeKey = $res.rows[rowNumberHere][someKey];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文