gpt4 book ai didi

javascript - 如何仅从 JSON 数据中获取最后 10 个对象

转载 作者:行者123 更新时间:2023-11-30 14:32:39 25 4
gpt4 key购买 nike

我有一个 .json 文件,我只想使用图形 Highcharts 从这个 json 文件中获取最后 10 个对象。

我的问题是脚本正在从数组中获取完整的对象,而我只想获取最后 10 个对象...

固定

我的代码是这样的:

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>

<div id="container"></div>
<script>
$.getJSON('https://cdn.rawgit.com/highcharts/highcharts/057b672172ccc6c08fe7dbb27fc17ebca3f5b770/samples/data/usdeur.json',
function (data) {

var currentValue = 0.9345;

Highcharts.chart('container', {
chart: {
zoomType: 'x'
},
title: {
text: 'USD to EUR exchange rate over time'
},
subtitle: {
text: document.ontouchstart === undefined ?
'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in'
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
text: 'Exchange rate'
},
plotLines: [{
value: currentValue,
color: 'green',
dashStyle: 'Dot',
width: 1,
label: {
text: currentValue,
textAlign: 'left',
x: -45
}
}]
},
legend: {
enabled: false
},
plotOptions: {
area: {
fillColor: {
linearGradient: {
x1: 0,
y1: 0,
x2: 0,
y2: 1
},
stops: [
[0, Highcharts.getOptions().colors[0]],
[1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
]
},
marker: {
radius: 2
},
lineWidth: 1,
states: {
hover: {
lineWidth: 1
}
},
threshold: null
}
},

series: [{
type: 'area',
name: 'USD to EUR',
data: data
}]
});
}
);
</script>

如果有人能帮助我就太好了:)

最佳答案

这很简单,只获取 json 数组 的最后 10 项,您可以根据索引使用 Array.filter() method 过滤它 像这样:

var last10 = data.filter(function(el, index) {
return index >= data.length - 10;
});

您只需要使用 index >= data.length - 10 过滤具有最后 10 个索引的项目。

演示:

在您的情况下,您只需要过滤 data 数组并将新过滤的 array 传递给图表:

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<script>
$.getJSON(
'https://cdn.rawgit.com/highcharts/highcharts/057b672172ccc6c08fe7dbb27fc17ebca3f5b770/samples/data/usdeur.json',
function(data) {

var last10 = data.filter(function(el, index) {
return index >= data.length - 10;
});

Highcharts.chart('container', {
chart: {
zoomType: 'x'
},
title: {
text: 'USD to EUR exchange rate over time'
},
subtitle: {
text: document.ontouchstart === undefined ?
'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in'
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
text: 'Exchange rate'
}
},
legend: {
enabled: false
},
plotOptions: {
area: {
fillColor: {
linearGradient: {
x1: 0,
y1: 0,
x2: 0,
y2: 1
},
stops: [
[0, Highcharts.getOptions().colors[0]],
[1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
]
},
marker: {
radius: 2
},
lineWidth: 1,
states: {
hover: {
lineWidth: 1
}
},
threshold: null
}
},

series: [{
type: 'area',
name: 'USD to EUR',
data: last10
}]
});
}
);
</script>

关于javascript - 如何仅从 JSON 数据中获取最后 10 个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50930557/

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