javascript - How to ration amount in purely functional style -


I need to distribute some quantity for the array of people according to my score. The rule is that their demands Anything that is done according to their condition is fulfilled. Javascript using Low-Dash ():

  var zodiac = 1000, people = [[name: 'joe', score: 40, demand: 400}, {name: '' Su 'Score: 30, Demand: 350}, {Name:' Kim ', Score: 25, Demand: 300}, {Name:' Ron ', Score: 20, Demand: 250}]; Function delivered (people, zodiac) {return _.map (_ (people, 'need'), function (person) {var ration = Math.man (person demand, amount); zodiac - = ration; // The state is (mutation) that I want to save from the return. Deal (person, {ration: ration}}}); } Console.log (Distribute (people, zodiac)); Result:  
  [{Demand: 400, name: "joe", ration: 400, score: 40}, {demand: 350, name} Name: "Roe", Ration: 350, Score: 30}, {Demand: 300, Name: "Kim", Ration: 250, Score: 25}, {Demand: 250, Name: "Ron", Ration: 0, Score : 20}]  

Since the solution depends on the sequence, is it possible to change this code to make it completely functional?

Functional solution is fold :

  Function delivered (people, zodiac) {back _. Fold (_. Sortbay (people, 'score'), function (result, person) {var ration = math.man (person demand, result. Remitting) var resultsperson = _.Sengwin (person, {ration: ration}) ; Return {people: results.co.co.uk (as a result), balance: results. Exit - ration};}, {people: [], balance: amount}). People; }  

Comments

Popular posts from this blog

GO: Serve static pages -

java - how to parse a JSON string into ObjectNode in Jackson? -

objective c - How to open front/back camera at the same time in iOS developing? -