javascript - Given an element and a list, how to functionally select the element after in the list? -


i'd understand how solve problem functionally (map/reduce/etc)

say have list of objects (i'm working in javascript @ moment):

l = [ {id: 43}, {id: 64}, {id: 12} ]  

and want function f gives next element in list.

f(l, {id:64}) => {id:12})  

and

f(l, {id:12}) => {id:43} 

i understand how solve imperatively, having hard time seeing how map/reduce. thank you!

not accounting things element being last item in collection, or not being in collection:

function findnext(collection, element) {     var current = collection.indexof(element);      return collection[current + 1]; } 

i don't see how reduce or map make better. unless want pass in id, , not object collection. it's:

function findnext(collection, id) {     var current = collection.map(function (e) { return e.id }).indexof(id);      return collection[current + 1]; } 

first version, working in jsfiddle: https://jsfiddle.net/cfxovfge/


Comments

Popular posts from this blog

python - jinja2: TemplateSyntaxError: expected token ',', got 'string' -

Qt4: how to send QString inside a struct via QSharedMemory -

node.js - NodeJS remote terminal to Dropbear OpenWRT-Server -