fork(5) download
  1. obj = [{"_id":"4940dgfd95jfsd9","score":15,"user":0,"type":"xxx"},
  2. {"_id":"4940dsdsvhjttt89","score":4,"user":0,"type":"yyy"},
  3. {"_id":"4941dgfdsdfggd9","score":7,"user":0,"type":"yyy"},
  4. {"_id":"4941dfds43rfsff9","score":21,"user":1,"type":"yyy"},
  5. {"_id":"4941dgfd93jfsd0","score":15,"user":1,"type":"xxx"},
  6. {"_id":"4942dgdsadvfd9","score":8,"user":1,"type":"yyy"},
  7. {"_id":"4942dgfdaefcod8","score":4,"user":2,"type":"yyy"},
  8. {"_id":"4942dgfdsarfsd1","score":32,"user":2,"type":"yyy"},
  9. {"_id":"4943dgfddasjfsd5","score":20,"user":2,"type":"xxx"},
  10. {"_id":"4943dasrtqxrfsd9","score":15,"user":3,"type":"xxx"},
  11. {"_id":"4943dgfooorafsd8","score":15,"user":3,"type":"yyy"},
  12. {"_id":"4944dgfddadfsd3","score":15,"user":3,"type":"yyy"},
  13. {"_id":"4944dgfddadfsd4","score":15,"user":3,"type":"yyy"},
  14. {"_id":"4944dgfd9dssdc4","score":15,"user":4,"type":"xxx"},
  15. {"_id":"4944dgdacbbrttt9","score":19,"user":4,"type":"yyy"},
  16. {"_id":"4945dgfsaddddd5","score":15,"user":4,"type":"xxx"}]
  17.  
  18. //obj.forEach(function(el){print(el)})
  19. //obj.forEach(function(el)print(el.user, el.type, el.score))
  20.  
  21. print("LENGTH BEFORE: ", obj.length)
  22.  
  23. var user, idx, count, score
  24. for (var i=obj.length - 1; i > -1; i--){
  25. if (obj[i].user !== user || i === 0){
  26. if (count > 1){
  27. idx.forEach(function(i){
  28. obj.splice(i, 1)
  29. });
  30. }
  31. user = obj[i].user
  32. count = 0
  33. score = Infinity
  34. }
  35. if (obj[i].type === 'yyy'){
  36. count++
  37. if (obj[i].score < score) {
  38. idx = [i]
  39. score = obj[i].score
  40. } else if (obj[i].score == score) {
  41. idx.push(i)
  42. }
  43. }
  44. }
  45.  
  46. print("LENGTH AFTER: ", obj.length)
  47. obj.forEach(function(el)print(el.user, el.type, el.score))
  48.  
  49. //obj.forEach(function(el){print(el)})
Success #stdin #stdout 0.48s 322816KB
stdin
Standard input is empty
stdout
LENGTH BEFORE:  16
LENGTH AFTER:  10
0 xxx 15
0 yyy 7
1 yyy 21
1 xxx 15
2 yyy 32
2 xxx 20
3 xxx 15
4 xxx 15
4 yyy 19
4 xxx 15