gpt4 book ai didi

javascript - 如何在 d3.group 中动态传递 ES6 其余参数

转载 作者:行者123 更新时间:2023-12-04 08:03:26 27 4
gpt4 key购买 nike

我正在尝试在 D3.js 版本 6 中对数据进行分组。

d3.group(data, d=> d.Header, d=>Group);
这工作正常,但如果我想在多列上动态分组数据,我该如何传递它?
function group(values, ...keys) {
return nest(values, _identity_js__WEBPACK_IMPORTED_MODULE_0__.default, _identity_js__WEBPACK_IMPORTED_MODULE_0__.default, keys);
}
在 d3.js 库中,它需要休息参数。有人可以帮助如何在函数中动态传递其余参数吗?

最佳答案

这种方法类似于您的答案,但它在对 d3.group 的调用中构建了函数数组。
我用过 groups在示例中只是这样它会记录,但只需更改为 group如果您需要将输出作为嵌套 Map :

const data = [
{"a": "a1", "b": "b1", "c": "c1", "d": "d1", "v": 1},
{"a": "a1", "b": "b1", "c": "c2", "d": "d1", "v": 11},
{"a": "a1", "b": "b2", "c": "c3", "d": "d1", "v": 21},
{"a": "a1", "b": "b2", "c": "c4", "d": "d1", "v": 31},
{"a": "a2", "b": "b3", "c": "c5", "d": "d1", "v": 41},
{"a": "a2", "b": "b3", "c": "c6", "d": "d1", "v": 51},
{"a": "a2", "b": "b4", "c": "c7", "d": "d1", "v": 61},
{"a": "a2", "b": "b4", "c": "c8", "d": "d1", "v": 71}
];

const groupMulti = (data, ...keys) => {
return d3.groups( //<--- change to group if you need Map as output
data,
...keys.map(k => (d => d[k]))
)
}

const arr = groupMulti(data, "a", "b");

console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.3.0/d3.min.js"></script>

关于javascript - 如何在 d3.group 中动态传递 ES6 其余参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66332228/

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