我正在尝试获取此数据集中“ min”列的平均值:
var data = [{
name: "Site A",
max: 202,
min: 0,
var: 202,
x: 1,
product: "Diesel",
case: "Base Case"
},
{
name: "Site B",
max: 145,
min: 147,
var: -2,
x: 0,
product: "Diesel",
case: "Base Case"
},
{
name: "Site C",
max: 221,
min: 238,
var: -17,
x: 0,
product: "Diesel",
case: "Base Case"
}]
您能告诉我我该怎么做吗?>
答案 0 :(得分:2)
您可以使用d3.mean()
,它将访问器函数作为第二个参数。该访问器函数将呈现给数组中的每个对象,并用于从中提取.min
属性。
const average = d3.mean(data, d => d.min);
看看这个工作演示:
var data = [{
name: "Site A",
max: 202,
min: 0,
var: 202,
x: 1,
product: "Diesel",
case: "Base Case"
},
{
name: "Site B",
max: 145,
min: 147,
var: -2,
x: 0,
product: "Diesel",
case: "Base Case"
},
{
name: "Site C",
max: 221,
min: 238,
var: -17,
x: 0,
product: "Diesel",
case: "Base Case"
}];
const average = d3.mean(data, d => d.min);
console.log(average);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>