数组中的Javascript数据库行-如何更改值?

发布于 2024-10-30 01:12:22 字数 555 浏览 1 评论 0原文

我有一个数据库查询,它将行返回到本地数组中:

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 技术交流群。

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

发布评论

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

评论(2

绝不放开 2024-11-06 01:12:22
for (var i=0; i < results.rows.length; i++) {
    localResultsArray[i] = results.rows.item(i);
}

正如已经指出的,item() 不太可能是一种方法,您可能指的是 item[i]。

localResultsArray[currentQuestionNumber].answered_correctly = 1;

如果 localResultsArray[currentQuestionNumber] 引用一个数组,则上面的行将answer_ Correctly 属性设置为 1。这是您想要做的吗?它不会更改任何数组中的值。您可能想要执行以下操作:

localResultsArray[answered_correctly] = 1;

localResultsArray[currentQuestionNumber] = 1;

取决于哪些变量引用了列号。

Javascript 数组只是具有特殊长度属性(和一些方便的方法)的对象,数组的成员只是具有数字名称(索引或键)的属性。因此,如果要访问数组的成员,请使用数字属性名称。使用字母名称会添加一个不属于数组成员的新属性。

for (var i=0; i < results.rows.length; i++) {
    localResultsArray[i] = results.rows.item(i);
}

As already pointed out, item() is unlikely to be a method, you probably meant item[i].

localResultsArray[currentQuestionNumber].answered_correctly = 1;

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:

localResultsArray[answered_correctly] = 1;

or

localResultsArray[currentQuestionNumber] = 1;

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.

薄荷→糖丶微凉 2024-11-06 01:12:22

通过快速查看您的代码,我想知道以下行是否正确:

localResultsArray[i] = results.rows.item(i);

我不知道您使用哪个库为您提供结果对象,但我高度怀疑 rows.item 是一个函数而不是一个数组。 ..

尝试

localResultsArray[i] = results.rows.item[i];

如果它不起作用,请使用您使用的库或结果对象上的更多数据更新您的帖子...

From a quick review of your code, I'm wondering if the following line is corret:

localResultsArray[i] = results.rows.item(i);

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

localResultsArray[i] = results.rows.item[i];

If it doesn't work, update your post with the library you used or some more data on the results object...

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