Dictionary/KeyValuePair in JavaScript?

Is there something similar to C# and VB.Net's Dictionary or KeyValuePair that is usable in JavaScript for RMMV? Or some other type of data structure where one doesn't need to iterate all values searching for one? I could use an array for what I want to accomplish, but after so many entries, that will start to get slow because I would need to iterate the entire array and return if the value was found, to say nothing of the need to regularly expand the array.


This isn't something that is a dire need for my project, but I keep circling back to it and thinking there must be a way to accomplish it.
@taarna23


Javascript has an "Object" type, which can be treated as a dictionary/hash


It's almost pretty much like how you'd do it in Python/Ruby:


var myData = {};
myData["test"] = 2
myData["animal"] = "cat"

myData["animal"] === "cat"
// true


Note that the basic Object has a number of limitations and so you should not expect to be able to use it like you would in other languages. For example, you can't use objects as keys.


ES6 appears to have an actual Map object which does allow for this.
Nope. Not at the moment, anyway. Object.values() or Object.entries() are not usable in RPG Maker MV's version of JavaScript. A for..in loop is the best, as far as I know (although I am relatively new to JavaScript, I could be totally wrong).


// Somesuch like this ...
var tmpKeys = Object.keys($gameMap);
for (key in tmpKeys) {
if ($gameMap[tmpKeys[key]] == '0') {
console.log(tmpKeys[key]);
break;
}
}


If you are creating your own object, I suppose you could have two arrays ... one for the key and the other for the value. As long as the indexes match up properly, you'd be fine.

Original thread: https://forums.rpgmakerweb.com/threads/dictionary-keyvaluepair-in-javascript.66928/