数组中的Javascript数据库行-如何更改值?
我有一个数据库查询,它将行返回到本地数组中:
for (var i=0; i < results.rows.length; i++)
{
localResultsArray[i] = results.rows.item(i);
}
稍后,我想更新本地数组中与数据库中的“answered_ Correctly”列相对应的值,但该值没有被更新。我的代码是:
localResultsArray[currentQuestionNumber].answered_correctly = 1;
但这不会由于某种原因将数组中的值更改为 1 - 我做错了什么?
(顺便说一句,如果我进行比较,例如在 if 语句中,那么它会起作用,所以我一定使用了上面的错误语法??)
if (localResultsArray[currentQuestionNumber].answered_correctly == 2)
{
incrementMe++;
}
提前致谢!缺口。
I have a database query that returns rows into a local array:
for (var i=0; i < results.rows.length; i++)
{
localResultsArray[i] = results.rows.item(i);
}
Later on, I want to update a value in the local array that corresponds to the 'answered_correctly' column in my database, but this value is not being updated. My code is:
localResultsArray[currentQuestionNumber].answered_correctly = 1;
but this doesn't change the value in the array to 1 for some reason - what am I doing wrong?
(Incidentally, if I do a comparison, eg. in an if statement then it works, so I must be using the wrong syntax above??)
if (localResultsArray[currentQuestionNumber].answered_correctly == 2)
{
incrementMe++;
}
Thanks in advance! Nick.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如已经指出的,item() 不太可能是一种方法,您可能指的是 item[i]。
如果 localResultsArray[currentQuestionNumber] 引用一个数组,则上面的行将answer_ Correctly 属性设置为 1。这是您想要做的吗?它不会更改任何数组中的值。您可能想要执行以下操作:
或
取决于哪些变量引用了列号。
Javascript 数组只是具有特殊长度属性(和一些方便的方法)的对象,数组的成员只是具有数字名称(索引或键)的属性。因此,如果要访问数组的成员,请使用数字属性名称。使用字母名称会添加一个不属于数组成员的新属性。
As already pointed out, item() is unlikely to be a method, you probably meant item[i].
If localResultsArray[currentQuestionNumber] references an array, then the above line sets the answered_correctly property to 1. Is that what you want to do? It will not change the value in any array. You may want to do:
or
depending on which of those variables references the column number.
Javascript arrays are just objects with a special length property (and some handy methods), the members of the array are just properties with numeric names (indexes or keys). So if you want to access the members of the array, use numeric property names. Using alphabetic names adds a new property that is not a member of the array.
通过快速查看您的代码,我想知道以下行是否正确:
我不知道您使用哪个库为您提供结果对象,但我高度怀疑 rows.item 是一个函数而不是一个数组。 ..
尝试
如果它不起作用,请使用您使用的库或结果对象上的更多数据更新您的帖子...
From a quick review of your code, I'm wondering if the following line is corret:
I don't know what library you use that gives you the results object, but I highly doubt rows.item is a function and not an array...
Try
If it doesn't work, update your post with the library you used or some more data on the results object...