Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a javascript equivalent of the Multimap data structure? [closed]

Multimap is an data structure that maps a key to a list/set of values.

Is there a good, unobtrusive js library that implements this data structure?

Edit - I know I can implement it "easily" myself, but I believe having it as a standalone abstraction is a good thing, so answers to this questions should not be "just implement it yourself".

like image 411
ripper234 Avatar asked Jul 04 '12 12:07

ripper234


1 Answers

Since @Esailija posted this as a comment only, I'll submit it as a possible answer. Objects using arrays as values is the way to go, and manipulate the values by way of Underscore.js.

var map = {
    foo: [1, 2, 3],
    bar: ['1', '2', '3']
};

map.foo = _.union(map.foo, [1, 4]); // map.foo -> [1, 2, 3, 4]

While it obviously depends on your needs, this approach gives you generic data structures that goes everywhere, and a library that works very nicely with collections and lists. For most purposes, performance of this approach should be just fine (just don't do it millions of times each second).

like image 78
Svend Avatar answered Sep 30 '22 15:09

Svend