gpt4 book ai didi

javascript - IndexedDB 中的算术运算

转载 作者:行者123 更新时间:2023-12-03 08:40:04 28 4
gpt4 key购买 nike

我需要你的帮助。我正在做我的大学作业。我正在研究 IndexedDB。我想对来 self 的 indexedDB 作为对象的数据进行一些加法、减法。我正在从 IndexedDB 检索值并将其保存以供将来应用算术运算时使用。

当我对数据求和时,它会连接数字。

For Example:

total += price[index].name; //in price array there is data like 100,50,20 my output should be sum of these values. but i am getting the output like this "1005020"

我不知道如何添加这些数据。这是我的代码。

function GetData(){
var output = document.getElementById("printOutput");
var product=[];
var price=[];
var person=[];
var index=0;
var total = 0;
var transaction = db.transaction("accounts", IDBTransaction.READ_WRITE);
var objectStore = transaction.objectStore("accounts");

var request = objectStore.openCursor();
request.onsuccess = function(evt) {

var cursor = evt.target.result;

if(cursor)
{
product.push({name: cursor.value.pType});
price.push({name: cursor.value.pPrice});
person.push({name: cursor.value.personID});
cursor.continue();
}
while(product.length!=index)
{
if(person[index].name==1){
var element = document.createElement("p");
element.textContent = product[index].name+"\t"+price[index].name;
output.appendChild(element);
total = total + price[index].name;
}
index++;
}
var sum = document.createElement("p");
sum.textContent = "Total Amount = " + total;
output.appendChild(sum);

};}

最佳答案

对于以后访问的人,通过评论解决,以下是解决方案:

Typically data is retrieved from IDB in form of String object, so your "+" operator is concatenating instead of adding. So, first convert your Strings into number and then perform addition. For conversion please refer here

关于javascript - IndexedDB 中的算术运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33064208/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com