Associative array in javascript using the / tuple pair as a multi-valued key / index

In python, I can use tuples as a dictionary key. What is 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 positions / normal pairs, but don't want to have duplicate pairs. My associative array will allow me to check if I have an existing pair [(posIdx,normalIdx)]for reuse or creation, if I do not.

I need some way to index using a two-digit key. I could just use a string, but that seems a little slower than two numbers.

+4
source share
2

Javascript , .

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

: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

, , , .

, .

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));
Hide result

, , .

0

Source: https://habr.com/ru/post/1529620/


All Articles