从数组中获取最后的 Api 数据
Getting the last Api data from a array
我的网站上有一个 covid 19 Api,但我希望它只提供最后的数据,数组每天都在变大,我该如何解决这个问题?
// Api Link+Key
$.getJSON("https://api.covid19api.com/total/country/netherlands",
// Function to extract data from the Api
function(data){
console.log(data);
// connect a variable to the Api Path
var covid_confirmed = data[231].Confirmed;
var covid_active = data[231].Active;
var covid_deaths = data[231].Deaths;
var covid_date = data[231].Date;
// Make the variable an working variable for in html
$('.covid_confirmed').append(covid_confirmed);
$('.covid_active').append(covid_active);
$('.covid_deaths').append(covid_deaths);
$('.covid_date').append(covid_date);
});
数组将这样形成:
Picture of RAW Api data
您可以获得最后一条记录。
const lastRecord = data.pop();
or
const lastRecord = data[data.length-1];
为什么不为 api 提供排序和分页选项?另一种解决方案是为最后一份报告创建一条路线,例如 /latest-result
只是select数组的最新值
// Api Link+Key
$.getJSON("https://api.covid19api.com/total/country/netherlands",
// Function to extract data from the Api
function(data){
console.log(data);
// connect a variable to the Api Path
var covid_confirmed = data[data.length-1].Confirmed;
var covid_active = data[data.length-1].Active;
var covid_deaths = data[data.length-1].Deaths;
var covid_date = data[data.length-1].Date;
// Make the variable an working variable for in html
$("#confirmed").text(`confirmed: ${covid_confirmed}`);
$("#active").text(`active: ${covid_active}`);
$("#deaths").text(`deaths: ${covid_deaths}`);
$("#date").text(`date: ${covid_date}`);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="confirmed"></div>
<div id="active"></div>
<div id="deaths"></div>
<div id="date"></div>
official documentation for covid19api.com 显示您可以将 from
和 to
参数添加到 API 调用。
这让您可以指定只需要自给定日期以来的数据,因此您可以使用调用
$.getJSON("https://api.covid19api.com/total/country/netherlands/status/confirmed?from=2020-09-08T23:00:00Z&to=2020-09-09T00:00:00Z")
只获取一个包含 2020 年 9 月 9 日数据的数组元素
我的网站上有一个 covid 19 Api,但我希望它只提供最后的数据,数组每天都在变大,我该如何解决这个问题?
// Api Link+Key
$.getJSON("https://api.covid19api.com/total/country/netherlands",
// Function to extract data from the Api
function(data){
console.log(data);
// connect a variable to the Api Path
var covid_confirmed = data[231].Confirmed;
var covid_active = data[231].Active;
var covid_deaths = data[231].Deaths;
var covid_date = data[231].Date;
// Make the variable an working variable for in html
$('.covid_confirmed').append(covid_confirmed);
$('.covid_active').append(covid_active);
$('.covid_deaths').append(covid_deaths);
$('.covid_date').append(covid_date);
});
数组将这样形成: Picture of RAW Api data
您可以获得最后一条记录。
const lastRecord = data.pop();
or
const lastRecord = data[data.length-1];
为什么不为 api 提供排序和分页选项?另一种解决方案是为最后一份报告创建一条路线,例如 /latest-result
只是select数组的最新值
// Api Link+Key
$.getJSON("https://api.covid19api.com/total/country/netherlands",
// Function to extract data from the Api
function(data){
console.log(data);
// connect a variable to the Api Path
var covid_confirmed = data[data.length-1].Confirmed;
var covid_active = data[data.length-1].Active;
var covid_deaths = data[data.length-1].Deaths;
var covid_date = data[data.length-1].Date;
// Make the variable an working variable for in html
$("#confirmed").text(`confirmed: ${covid_confirmed}`);
$("#active").text(`active: ${covid_active}`);
$("#deaths").text(`deaths: ${covid_deaths}`);
$("#date").text(`date: ${covid_date}`);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="confirmed"></div>
<div id="active"></div>
<div id="deaths"></div>
<div id="date"></div>
official documentation for covid19api.com 显示您可以将 from
和 to
参数添加到 API 调用。
这让您可以指定只需要自给定日期以来的数据,因此您可以使用调用
$.getJSON("https://api.covid19api.com/total/country/netherlands/status/confirmed?from=2020-09-08T23:00:00Z&to=2020-09-09T00:00:00Z")
只获取一个包含 2020 年 9 月 9 日数据的数组元素