Find adjacent entries for a javascript Map? -
given
var mymap = new map() mymap.set("alpha", "first") mymap.set("beta", "second") mymap.set("charlie", "third") mymap.set("delta", "fourth") mymap.set("echo", "fifth") mymap.set("foxtrot", "sixth") starting given key...
mymap.get("charlie") //=> "third" ... how values surrounding keys?
// pseudo code mymap.get("charlie").prev() //=> "second" mymap.get("charlie").next() //=> "fourth" ideally, i'd while iterating on mymap:
var valuesicareabout = []; mymap.foreach(function(value, key, map) { valuesicareabout.push({ callsign: key, callvalue: value, nextcallvalue: map.get(key).next(), // pseudo code prevcallvalue: map.get(key).prev() // pseudo code }); }); in actual use case, keys of map dom nodes, , need siblings in order calculate position offsets. expected prev value first node in map null , next value last node in map null.
that make sense?
how iterating on keys of map array?
var keys = array.from(map.keys()); keys.foreach(function(key, i, keys) { var current = map.get(key); var previous = map.get(keys[i-1]); var next = map.get(keys[i+1]); // ... }); (obviously have account first , last element here)
if internal code, think extending existing map class own foreach implementation passes previous , next value callback.
Comments
Post a Comment