素胚勾勒不出你
2023-12-14 14:35:07
有沒(méi)有辦法編寫(xiě)這種拾取/映射操作edges = edges.map(([v1, v2]) => [vertices[v1], vertices[v2]]);// exampleconst edges = [ [0, 1], [1, 0]];const vertices = [ [0, 0], [1, 1]]; ~~~>[ [ [0, 0], [1, 1] ], [ [1, 1], [0, 0] ]]更優(yōu)雅,也許使用lodash?我迄今為止最好的解決方案是這樣的:function pickMap(edges, vertices) { return ld.invokeMap(edges, "map", ld.propertyOf(vertices));}但也許我錯(cuò)過(guò)了一些更基本的程序。就像是edges.map(([v1, v2]) => ld.pick(vertices, [v1, v2]))很接近,但它返回一個(gè)帶有錯(cuò)誤鍵 (v1, v2) 而不是 (0, 1) 的對(duì)象數(shù)組。
1 回答

瀟瀟雨雨
TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以_.at()使用索引數(shù)組來(lái)獲取值數(shù)組:
const edges = [[0, 1],[1, 0]];
const vertices = [[0, 0],[1, 1]];
const result = edges.map(v => _.at(vertices, v));
console.log(JSON.stringify(result));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>
添加回答
舉報(bào)
0/150
提交
取消