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

python - Using tuple as a key in a dictionary in javascript - Stack Overflow

programmeradmin0浏览0评论

In python I have a Random dictionary where I use tuple as a key and each is mapped to some value.

Sample

Random_Dict = {
    (4, 2): 1,
    (2, 1): 3,
    (2, 0): 7,
    (1, 0): 8
}

example in above key: (4,2) value: 1

I am attempting to replicate this in Javascript world

This is what I came up with

const randomKeys = [[4, 2], [2, 1], [2, 0], [1, 0] ]

const randomMap = {}

randomMap[randomKeys[0]] = 1
randomMap[randomKeys[1]] = 3
randomMap[randomKeys[2]] = 7
randomMap[randomKeys[3]] = 8
randomMap[[1, 2]] = 3

I am wondering if this is the most effective way. I almost wonder if i should do something like holding two numbers in one variable so that way i can have a dictionary in JS that maps 1:1. Looking for suggestions and solutions that are better

In python I have a Random dictionary where I use tuple as a key and each is mapped to some value.

Sample

Random_Dict = {
    (4, 2): 1,
    (2, 1): 3,
    (2, 0): 7,
    (1, 0): 8
}

example in above key: (4,2) value: 1

I am attempting to replicate this in Javascript world

This is what I came up with

const randomKeys = [[4, 2], [2, 1], [2, 0], [1, 0] ]

const randomMap = {}

randomMap[randomKeys[0]] = 1
randomMap[randomKeys[1]] = 3
randomMap[randomKeys[2]] = 7
randomMap[randomKeys[3]] = 8
randomMap[[1, 2]] = 3

I am wondering if this is the most effective way. I almost wonder if i should do something like holding two numbers in one variable so that way i can have a dictionary in JS that maps 1:1. Looking for suggestions and solutions that are better

Share Improve this question asked Mar 4, 2020 at 21:42 DineroDinero 1,1604 gold badges25 silver badges53 bronze badges 5
  • 3 Note that the actual key for e.g. [1, 2] would be "1,2"; non-string keys are stringified. – jonrsharpe Commented Mar 4, 2020 at 21:46
  • what data structure do you want? – Nina Scholz Commented Mar 4, 2020 at 21:52
  • @NinaScholz in python i can have a dictionary that uses tuple as a key. That is what i am looking for – Dinero Commented Mar 4, 2020 at 21:53
  • For things like this I usually find it better to create a class like "TupleKeyDict" with add/get methods to mimic the behavior you get in python. – user2263572 Commented Mar 4, 2020 at 21:54
  • tuple is no type of javascript. do you know an equivalent of it? – Nina Scholz Commented Mar 4, 2020 at 21:54
Add a ment  | 

2 Answers 2

Reset to default 5

You can use a Map to map sets of 2 arbitrary values. In the following snippet the keys can be 'tuples' (1), or any other data type, and the values can be as well:

const values = [
  [ [4, 2], 1],
  [ [2, 1], 3],
  [ [2, 0], 7],
  [ [1, 0], 9],
];

const map = new Map(values);


// Get the number corresponding a specific 'tuple'
console.log(
  map.get(values[0][0]) // should log 1
);

// Another try:
console.log(
  map.get(values[2][0]) // should log 7
);

Note that the key equality check is done by reference, not by value equivalence. So the following logs undefined for the above example, although the given 'key' is also an array of the shape [4, 2] just like one of the Map keys:

console.log(map.get([4, 2]));

(1) Tuples don't technically exist in Javascript. The closest thing is an array with 2 values, as I used in my example.

You can do it this way:

const randomKeys = {
    [[4, 2]]: 1,
    [[2, 1]]: 3,
    [[2, 0]]: 7,
    [[1, 0]]: 8
}
console.log(randomKeys[ [4,2] ]) // 1 

[] in objects property is used for dynamic property assigning. So you can put an array in it. So your property will bee like [ [4,2] ] and your object key is [4,2].

发布评论

评论列表(0)

  1. 暂无评论