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}
Question
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.
Solution
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}
OTHER TIPS
You can use a Map: https://developer.mozilla.org/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.