I would add that Map and Set come standard with modern JS, which enables you to create hashtables out of object references, not just strings. This allows you to pass any object, primitive type, null or undefined as the key with any other value. As a bonus, they both expose Iterator interfaces.
consta={};// this is one object with its own refconstb={};// this is another object with a different refconstmyMap=newMap();myMap.set(a,'object a');myMap.set(b,'object b');myMap.has({});// returns `false`myMap.has(a);// returns `true`myMap.get(b);// returns 'object b'constmySet=newSet();mySet.add(a);mySet.has(a);// returns `true`mySet.has(b);// returns `false`mySet.has({});// returns `false`// this will print out 'object a' and 'object b'for(let[key,value]ofmyMap){console.log(value);}
Oh wow, does that mean this does a deep comparison of the object? Wondering if I might want to start using this to find objects in an array of objects (as opposed to finding it by a property comparison). 1. I think it would be easier in some cases, and 2. wondering if this assigns a hash behind the scenes and has a lower time complexity.
No, it's not a deep comparison, it's equivalent to === between values, just like indexOf() on arrays. And yes, internally within the VM, every object has a unique ID which is used for comparisons using strict equality, but it isn't available as an exposed value in JS.
You're probably talking about an object const hashtable = {}.
In JS, everything is an object (like in Ruby), and here you can store any key/value pair.
Objects are very useful in any occasion unless you just need a single variable or a list (array). Of course I'm not extending to classes and O.
You access object's data using the key you need const age = person.age //or person['age'] and set it's value the same way person.name = "john" //person["name"] = "john".
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (4)
I would add that
Map
andSet
come standard with modern JS, which enables you to create hashtables out of object references, not just strings. This allows you to pass any object, primitive type,null
orundefined
as the key with any other value. As a bonus, they both expose Iterator interfaces.Oh wow, does that mean this does a deep comparison of the object? Wondering if I might want to start using this to find objects in an array of objects (as opposed to finding it by a property comparison). 1. I think it would be easier in some cases, and 2. wondering if this assigns a hash behind the scenes and has a lower time complexity.
No, it's not a deep comparison, it's equivalent to
===
between values, just likeindexOf()
on arrays. And yes, internally within the VM, every object has a unique ID which is used for comparisons using strict equality, but it isn't available as an exposed value in JS.For an in-depth look at that, see dev.to/krofdrakula/searching-throu...
You're probably talking about an object
const hashtable = {}
.In JS, everything is an object (like in Ruby), and here you can store any key/value pair.
Objects are very useful in any occasion unless you just need a single variable or a list (array). Of course I'm not extending to classes and O.
You access object's data using the key you need
const age = person.age //or person['age']
and set it's value the same wayperson.name = "john" //person["name"] = "john"
.