最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dictionary - Associative array in javascript using a pairtuple as multi-value keyindex - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Mar 2, 2014 at 4:53 jozxyqk asked Mar 2, 2014 at 3:40 jozxyqkjozxyqk 17.5k15 gold badges98 silver badges197 bronze badges 4
  • There isn't really one currently (an uping version of JavaScript is slated to include one) -- is there any particular reason to do so? – Qantas 94 Heavy Commented Mar 2, 2014 at 3:44
  • What are you trying to do exactly? I mean, you could actually use a string representation of your tuple as a key to the array, but the thought of it is kind of painful. You could also use an actual 2 dimensional associative array as well as Stepan mentions in his answer. I think you'll get a better answer if you say what you are trying to do, and maybe show how you are using the above sort of datastructures. – barry-johnson Commented Mar 2, 2014 at 3:50
  • Can there be multiple normals for any one position or vice versa? – Ja͢ck Commented Mar 2, 2014 at 3:56
  • @Jack yes, this is exactly the reason why I need the two-value key. My application needs position/normals on a 1 to 1 ratio, but the data gets bigger if I don't reuse existing pairs. – jozxyqk Commented Mar 2, 2014 at 4:09
Add a ment  | 

2 Answers 2

Reset to default 5

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}

You can use a Map: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

The keys of an Object are Strings and Symbols, whereas they can be any value for a Map, including functions, objects, and any primitive.

This will avoid a potential problem with the key just being a string representation of the array.

d = {};
d[[1,2]] = 5;
d[[2,1]] = 6;
console.log("d[[1,2]]:", d[[1,2]]);
console.log("d['1,2']:", d['1,2']);

m = new Map();
ak1 = [1,2];
ak2 = [1,2];
m.set([1,2], 5);
m.set([2,1], 6);
m.set(ak1, 7);
console.log("m.get('1,2'):", m.get('1,2'));
console.log("m.get([1,2]):", m.get([1,2]));
console.log('m.get(ak1):', m.get(ak1));
console.log('m.get(ak2):', m.get(ak2));

Note that it uses a variable reference for the key, not the value.

发布评论

评论列表(0)

  1. 暂无评论