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

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -