I want to find the maximum value in a JSON object. This is my data-
(3) [{…}, {…}, {…}]
0: {name: "a", value: 12}
1: {name: "b", value: 28}
2: {name: "c", value: 60}
Run Code Online (Sandbox Code Playgroud)
I'd like to get the maximum value, but they're nested. I could do something like this
arr = {}
for (x in jsonObj){
arr.push(x.value)
}
d3.max(arr)
Run Code Online (Sandbox Code Playgroud)
I was curious if there's a better way of dealing with this situation. My goal is to use d3.max() to find the highest value.
尽管Hrishi和Nina Scholz的其他答案都很好,但我想提一下使用D3的惯用方式是利用d3.max()。您可以传入一个访问器函数作为第二个参数,该参数将为数组的每个成员调用,并返回您感兴趣的嵌套值。
const data = [
{name: "a", value: 12},
{name: "b", value: 28},
{name: "c", value: 60}
];
const maxValue = d3.max(data, d => d.value);
// accessor function ---^
console.log(maxValue); // 60Run Code Online (Sandbox Code Playgroud)
<script src="https://d3js.org/d3.v5.js"></script>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71 次 |
| 最近记录: |