javascript中的关联数组,使用pair / tuple作为多值键/索引(Associative array in javascript using a pair/tuple as multi

编程入门 行业动态 更新时间:2024-10-28 06:26:18
javascript中的关联数组,使用pair / tuple作为多值键/索引(Associative array in javascript using a pair/tuple as multi-value key/index)

在python中,我可以使用元组作为字典的关键。 什么是javascript中的等价物?

>>> d = {} >>> d[(1,2)] = 5 >>> d[(2,1)] = 6 >>> d {(1, 2): 5, (2, 1): 6}

对于那些有兴趣的人,我有两个阵列......

positions = ...

normals = ...

我需要制作第三个位置/正常对数组,但不希望有重复对。 我的关联数组将让我检查是否有一个现有的[(posIdx,normalIdx)]对重用或创建一个如果我没有。

我需要一些使用双值键索引的方法。 我可以使用一个字符串,但这似乎比两个数字慢一点。

In python, I can use tuples as the key to a dictionary. What's the equivalent in javascript?

>>> d = {} >>> d[(1,2)] = 5 >>> d[(2,1)] = 6 >>> d {(1, 2): 5, (2, 1): 6}

For those who are interested, I have two arrays...

positions = ...

normals = ...

I need to make a third array of position/normal pairs, but don't want to have duplicate pairs. My associative array would let me check to see if I have an existing [(posIdx,normalIdx)] pair to reuse or create one if I don't.

I need some way of indexing using a two-value key. I could just use a string, but that seems a bit slower than two numbers.

最满意答案

Javascript没有元组,但您可以使用数组。

>>> d = {} >>> d[[1,2]] = 5 >>> d[[2,1]] = 6 >>> d Object {1,2: 5, 2,1: 6}

Javascript does not have tuples but you can use arrays instead.

>>> d = {} >>> d[[1,2]] = 5 >>> d[[2,1]] = 6 >>> d Object {1,2: 5, 2,1: 6}

更多推荐

本文发布于:2023-07-31 11:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1343810.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   索引   tuple   javascript   pair

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!