我的代码收集sell值。当我运行我的代码,它打破了,因为有日期键在第一行,它不算作数组。当我删除日期键,它的工作,但日期键必须留在那里;我不能更改json.
let data = {
Update_Date: "2023-03-15 14:00:01",
USD: {
Come: "18,9814",
Type: "Currency",
Sell: "18,9839",
Change: "%0,04",
},
EUR: {
Come: "20,1690",
Type: "Currency",
Sell: "20,1777",
Change: "%-0,94",
}
};
let t=0;
for (key in data) {
if(data[key] != "Update_Date"){
parseInt(data[key].Sell);
const sell1=parseFloat(String(data[key].Sell).replace(",","."));
t+=sell1;
console.log(toplam);
}
}
I have try, !=, data。key instanceof Array == true用于转义,但它不起作用。我能做什么?
你是在比较值,而不是键。
将if
语句中的data[key]
改为key
let data = {
Update_Date: "2023-03-15 14:00:01",
USD: {
Come: "18,9814",
Type: "Currency",
Sell: "18,9839",
Change: "%0,04",
},
EUR: {
Come: "20,1690",
Type: "Currency",
Sell: "20,1777",
Change: "%-0,94",
}
};
let t = 0;
for (key in data) {
if(key != "Update_Date"){
const sell1 = parseFloat(data[key].Sell.replace(",","."));
t += sell1;
}
}
console.log(t);