我正在尝试从URL获取JSON数据并存储在sqlite db.From sqlite db我将数据转换为JSON对象数组并使用该值我试着执行if if else条件我有n没有在JSON created_at和field3中的对象是要在此执行的数据如果field3为0我必须忽略,如果field3为1,我们必须计算从该obj日期n时间到下一个obj的日期n时间差异,其中field3结束1.我的代码如下。
function renderList1(tx,result){
newJson = [ ];
if (result != null && result.rows != null) {
for (var k = 0; k < result.rows.length; k++) {
var row = result.rows.item(k);
newJson[k] = { created_at:row.Created_at,entry_id:row.entry_id,field1:row.Field1,field2:row.Field2,field3:row.Field3 };
}
}
var jsonAll = JSON.stringify(newJson);
console.log(jsonAll);
var feeds = $.parseJSON(jsonAll);
console.log(feeds);
power();
function power(){
for (var i=0;i<feeds.length;i++) //feeds contain the JSON data
{
if(feeds[i]["field3"] === "1"){
//Start Time
var d1 = new Date(feeds[i]["created_at"]);
console.log(d1);
for (var j=i+1;j<feeds.length;j++) //compare objects starting from 'i' till next "1" is found
{
if(feeds[j]["field3"] === "0")
{
//End Time
var d2 = new Date(feeds[j]["created_at"]);
console.log(d2);
j=feeds.length;
}
}
//Time difference in milli seconds
var diff = d2.getTime() - d1.getTime();
var diffInHours= diff / 1000diff/ 3600 / 1000;
console.log(diffInHours);
var pow = Powercon * diffInHours;
console.log(pow);
}else{
console.log("Field3 is 0");
}
}
}
JSON数据:
[
{
"created_at": "2017-04-12T08:53:46Z",
"entry_id": 37368,
"field1": "14",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T09:03:54Z",
"entry_id": 37369,
"field1": "14",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T09:14:02Z",
"entry_id": 37370,
"field1": "14",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T09:24:03Z",
"entry_id": 37371,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T09:34:06Z",
"entry_id": 37372,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T09:44:09Z",
"entry_id": 37373,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T09:54:12Z",
"entry_id": 37374,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T10:04:20Z",
"entry_id": 37375,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T10:24:20Z",
"entry_id": 37376,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T10:44:27Z",
"entry_id": 37377,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T10:54:30Z",
"entry_id": 37378,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T11:04:34Z",
"entry_id": 37379,
"field1": "16",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T11:14:37Z",
"entry_id": 37380,
"field1": "16",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T11:24:40Z",
"entry_id": 37381,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T11:34:43Z",
"entry_id": 37382,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T11:44:46Z",
"entry_id": 37383,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T11:54:49Z",
"entry_id": 37384,
"field1": "16",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T12:04:53Z",
"entry_id": 37385,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T12:14:56Z",
"entry_id": 37386,
"field1": "15",
"field2": "0",
"field3": "0"
},
{
"created_at": "2017-04-12T12:25:00Z",
"entry_id": 37387,
"field1": "16",
"field2": "0",
"field3": "0"
}
]
我尝试上面的代码,当我运行我遇到问题在if和else条件如果field3等于1它应该执行如果条件但它将去其他条件。任何人可以帮我在哪里出错? ?三江源
答案 0 :(得分:1)
if(feeds[i]["field3"] === 1){
应该解决问题。它被视为整数。此外,您应该考虑使用更高阶函数或lodash.js,这对数组操作非常有用,并使您的代码更易于管理和读取。