好吧我的问题是我在脚本中加载了这个json文件。我也在使用d3.js
[{"name":"object1","income":[[2013,100], [2014, 450], [2015,175]]}, {"name":"object2","income":[[2013,230], [2014, 250], [2015,375]]}]
收入数组由一年和收入值组成。 [2013,100]暗示2013年的收入等于100.我的问题是我想获得数据集收入的最大值。在这种情况下,最大值等于450。 是否可以使用d3.max函数
执行此操作非常感谢。
答案 0 :(得分:2)
普通的旧javascript怎么样;
var max = 0;
var dataset = [{"name":"object1","income":[[2013,100], [2014, 450], [2015,175]]}, {"name":"object2","income":[[2013,230], [2014, 250], [2015,375]]}];
dataset.forEach(function(obj) {
obj.income.forEach(function(arr) {
var val = arr[1];
if(val > max) {
max = val;
}
});
});
console.log(max);
答案 1 :(得分:1)
在现代浏览器中
var data = [{
"name": "object1",
"income": [
[2013, 100],
[2014, 450],
[2015, 175]
]
}, {
"name": "object2",
"income": [
[2013, 230],
[2014, 250],
[2015, 375]
]
}];
var max = Math.max.apply(Math, data.map(function(item) {
return item.income.reduce(function(a, b) {
return a > b[1] ? a : b[1]
}, 0)
}));
snippet.log(max)
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
答案 2 :(得分:1)
是的,你可以很容易地使用D3,使用d3.max
的第二个参数,它接受一个元素并返回它的一部分以取最大值:
var maxIncome = d3.max(data, function(d) {
return d3.max(d.income, function(e) { return e[1]; });
});
答案 3 :(得分:0)
一个解决方案,问题是我不清楚,获得每年的最高收入,或所有年份的最高收入。
Sub Marco()
Dim x As Integer
Dim sh As Worksheet
'Used to remove unwanted characters from text within cells
Cells.Replace What:="'", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:=",", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
'Counts rows within column "A"
NumRows = Range("A1", Range("A1").End(xlDown)).Rows.Count
'Selects Cell "C1" to process the proceeding loop
Range("C1").Select
'A loop that carries out given functions for all cells with data.
For x = 1 To NumRows
'Creates a count if statement for all duplicate information
ActiveCell.FormulaR1C1 = "=COUNTIF(C1,RC1)"
ActiveCell.Select
'Moves down to next cell in column
ActiveCell.Offset(1, 0).Select
Next
'Changes all forumlas to values
For Each sh In ActiveWorkbook.Worksheets
With sh.UsedRange
.Value = .Value
End With
Next sh
'Removes all duplicate values only leaving require information
ActiveSheet.Range("$A$1:$C$500").RemoveDuplicates Columns:=Array(1, 2), Header _
:=xlYes
Range("A1").Select
End Sub
&#13;