Dictionary/KeyValuePair in JavaScript?

taarna23

Marshmallow Princess
Global Mod
Joined
Jul 20, 2012
Messages
2,349
Reaction score
4,631
First Language
English
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.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,230
Reaction score
3,071
First Language
English
@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.
 
Last edited by a moderator:

Ossra

Formerly Exhydra
Veteran
Joined
Aug 21, 2013
Messages
934
Reaction score
682
First Language
English
Primarily Uses
RMMV
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.
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Why is it so hard to accept existential stoicism? To insist stoics must be deterministic is like insisting God couldnt create evolving animals. Drop the dogmas and concider the compatible mechanics
I just had a duel at Duel Links, and started right away with a fusion monster... felt like an actual yugioh main character :3
Now listen closely... Here's a little lesson in trickery, This is going down in history...
So the four of my friends are singing a song and I'm like... ohoho I don't know this song. Better act like I do!
A boss that spams confuse, blind, silence, oil and slow on all party members? Good idea FF12 *headdesk*

Forum statistics

Threads
93,511
Messages
913,114
Members
123,045
Latest member
califert
Top