目前我正在编写一个从Police.UK数据库中提取的程序,以下是从API请求时数据的解释方式。数据传递到循环中并存储值,如下所示。我认为问题出在var outcome = data[i]["outcome_status"];
之间。问题是结果状态可以是null
还是包含两个字符串outcome_status.date
和outcome_status.category
,我如何区分和确定天气值为null或包含更多信息。
谢谢!
function create_marker(lat, lng, cat, time, id, street, outcome) {
completed_requests = 1;
document.getElementById("popular_crime").innerText = "";
document.getElementById("num_of_crimes").innerText = "";
var current_lat_lng = [];
current_lat_lng.push(lat, lng, cat, time, id, street, outcome);
chunksize = 7;
var chunks = [];
current_lat_lng.forEach((item) => {
if (!chunks.length || chunks[chunks.length - 1].length == chunksize)
chunks.push([]);
chunks[chunks.length - 1].push(item);
});
for (var z = 0; z < chunks.length; z++) {
var a = chunks[z];
var icons = L.icon({
iconSize: [32, 32],
iconUrl: 'asset/images/' + a[2] + '.png'
});
}
switch (a[2]) {
case 'antisocialbehaviour':
var popup2 = "<center><br><h2>Anti Social Behaviour</h2><img src=\"asset/images/antisocialbehaviour.png\"height=\"42\" width=\"42\"><h3>Crime Identity Number:</h3><p>" + id + "</p><h3>Month:</h3><p>" + time + "</p><h3>Street Name:</h3><p>" + street + "</p><h2><h3>Outcome:</h3><p>" + outcome + "</p><h2></center>";
var marker = L.marker(new L.LatLng(a[0], a[1]), {
icon: icons,
time: a[3],
id: a[4],
street: a[5],
outcome: a[6]
});
marker.addTo(group1).bindPopup(popup2);
asbcount++
break;
break;
default:
break;
}
function get_JSON(url, callback_func) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
var check1 = xmlhttp.readyState == 4 && this.status == 200;
var check2 = check1 && typeof callback_func === "function";
if (check2) {
json_obj = JSON.parse(this.responseText);
callback_func(json_obj);
}
}
console.log("Getting stats for", url)
xmlhttp.open("POST", url, true);
xmlhttp.send();
}
function mode(c) {
var popular_str = "";
var popular_num = 0;
for (item in c) {
num = c[item]
if (num > popular_num) {
popular_str = item;
popular_num = num;
}
}
return popular_str;
}
function JSON_callback(data) {
completed_requests++;
var data_len = data.length;
if (data[0] != undefined) {
for (var i = 0; i < data_len; i++) {
cat1 = data[i]["category"];
cat = cat1.replace(/-/g, "");
cat3 = cat1.replace(/-/g, " ");
id = data[i]["persistent_id"];
lat = data[i]["location"]["latitude"];
lng = data[i]["location"]["longitude"];
street = data[i]["location"]["street"]["name"];
time = data[i]["month"];
var outcome = data[i]["outcome_status"];
if (cat3 in crimes) {
crimes[cat3]++;
} else {
crimes[cat3] = 1;
}
drawChart();
console.log(data)
committed = true;
create_marker(lat, lng, cat, time, id, street, outcome);
num_of_crimes++;
}
}
document.getElementById("num_of_crimes").innerText = num_of_crimes;
if (completed_requests == 1) {
console.log("Requests done");
//console.log(crimes);
if (committed) {
document.getElementById("popular_crime").innerText = mode(crimes);
} else {
document.getElementById("popular_crime").innerText = "None";
}
}
}
以下是来自API的JSON外观
[
{
"category":"anti-social-behaviour",
"location_type":"Force",
"location":{
"latitude":"53.746850",
"street":{
"id":1289421,
"name":"On or near Denshaw Drive"
},
"longitude":"-1.589005"
},
"context":"",
"outcome_status":null,
"persistent_id":"a42e1e83ed8055156c1cae319a38b590957de163e8eaeb7884c2b7dd1491056d",
"id":55436628,
"location_subtype":"",
"month":"2017-03"
},
{
"category":"burglary",
"location_type":"Force",
"location":{
"latitude":"53.746074",
"street":{
"id":1289411,
"name":"On or near Hull Street"
},
"longitude":"-1.596109"
},
"context":"",
"outcome_status":{
"category":"Unable to prosecute suspect",
"date":"2017-03"
},
"persistent_id":"906849d248a5b0b26857184a291cbd1e952dfbe62783ad59fdad1c58737386ec",
"id":55411246,
"location_subtype":"",
"month":"2017-03"
}
]
答案 0 :(得分:2)
如果提供的Object.assign
为outcome
,您可以使用null
将var data = [{
"category": "anti-social-behaviour",
"location_type": "Force",
"location": {
"latitude": "53.746850",
"street": {
"id": 1289421,
"name": "On or near Denshaw Drive"
},
"longitude": "-1.589005"
},
"context": "",
"outcome_status": null,
"persistent_id": "a42e1e83ed8055156c1cae319a38b590957de163e8eaeb7884c2b7dd1491056d",
"id": 55436628,
"location_subtype": "",
"month": "2017-03"
},
{
"category": "burglary",
"location_type": "Force",
"location": {
"latitude": "53.746074",
"street": {
"id": 1289411,
"name": "On or near Hull Street"
},
"longitude": "-1.596109"
},
"context": "",
"outcome_status": {
"category": "Unable to prosecute suspect",
"date": "2017-03"
},
"persistent_id": "906849d248a5b0b26857184a291cbd1e952dfbe62783ad59fdad1c58737386ec",
"id": 55411246,
"location_subtype": "",
"month": "2017-03"
}
];
for (var i = 0; i < 2; i++) {
var outcome = Object.assign({
category: "",
date: ""
}, data[i]["outcome_status"]);
console.log(outcome);
}
设置为某些默认值。
outcome = outcome.date + ":" + outcome.category;
如果您希望结果只是一个字符串,您可以使用
<input type="text" name="mail" id="mail">
或其他取决于你的欲望。