Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. var array=[
  2. {price:0.112,qty:1},
  3. {price:0.218,qty:1},
  4. {price:0.216,qty:1},
  5. {price:0.321,qty:1},
  6. {price:0.325,qty:2},
  7. {price:0.335,qty:2},
  8. {price:0.536,qty:1}
  9. ]
  10.  
  11. [
  12. {price:0.32,qty:3},
  13. {price:0.33,qty:2},
  14. {price:0.53,qty:1}
  15. ]
  16.  
  17. [
  18. {price:0.3,qty:5},
  19. {price:0.5,qty:1}
  20. ]
  21.  
  22. let result = [];
  23. array.forEach(function(a) {
  24. a.price = a.price.toFixed(1);
  25. if (result.some(x => x.price === a.price)) {
  26. result.find(x => x.price === a.price).qty += a.qty;
  27. } else {
  28. result.push(a);
  29. }
  30. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement