Nishanth J

Nishanth J

  • NA
  • 855
  • 13.8k

Simplifying Known Vanilla JS to Advanced ES

Jan 3 2021 4:27 PM
All The below codding snippets works perfectly. Need to make sure that the below snippets of code can be simplified using any of an advanced ECMA standards
  1. let pauseAllcount = 0, resumeAllcount = 0;  
  2. this.xUserDetails.forEach((el, indx) => {  
  3.     if (this.xUserDetails[indx].userProctorSignal === 1) {  
  4.         pauseAllcount++;  
  5.     } else if (this.xUserDetails[indx].userProctorSignal === 0) {  
  6.         resumeAllcount++;  
  7.     }  
  8. });  
The above snippets could be written as
  1. let pauseAllcount = 0, resumeAllcount = 0;  
  2. pauseAllcount = this.xUserDetails.filter(x => x.userProctorSignal === 1).length;  
  3. resumeAllcount = this.xUserDetails.filter(x => x.userProctorSignal === 0).length;  
Results will be one and same. But I'm afraid that iterating over the same array twice, I might risk the performance overhead cost by O(2n). Will this same block of code couldn't be written to reduce no of lines and replaced by advanced ECMA approach giving optimistic solutions?...
Likewise showing all the below 5 coding snippets could be simplified with advanced ECMA
I Code:
  1. if (this.pageReqDetails.RowTot === this.xUserDetails.length) {  
  2. if (this.userdetails.every(x => x.extensionTime === this.userdetails[0].extensionTime)) {  
  3. this.timeForallUser = this.userdetails[0].extensionTime;  
  4. }  
  5. if (pauseAllcount === this.xUserDetails.length) {  
  6. this.toggleButtonDisplayType = 1;  
  7. else if (resumeAllcount === this.xUserDetails.length) {  
  8. this.toggleButtonDisplayType = 0;  
  9. this.timeForallUser = 0;  
  10. else {  
  11. this.toggleButtonDisplayType = -1;  
  12. this.timeForallUser = 0;  
  13. }  
  14. else {  
  15. this.timeForallUser = 0;  
  16. }  
II Code:
  1. let flagPauseAll = false, flagResumeAll = false;  
  2. if (this.pageReqDetails.RowTot === this.xUserDetails.length) {  
  3. this.xUserDetails.forEach((el, indx) => {  
  4. if (type === 0) {  
  5. if (el.extensionTime === time) {  
  6. this.timeForallUser = 0;  
  7. this.toggleButtonDisplayType = 0;  
  8. else {  
  9. flagResumeAll = true;  
  10. }  
  11. else if (type === 1) {  
  12. if (el.extensionTime === time) {  
  13. this.timeForallUser = time;  
  14. this.toggleButtonDisplayType = 1;  
  15. else {  
  16. flagPauseAll = true;  
  17. }  
  18. }  
  19. });  
  20. }  
  21. if (flagPauseAll || flagResumeAll) {  
  22. this.timeForallUser = 0;  
  23. this.toggleButtonDisplayType = -1;  
  24. }  
III Code:
  1. let countAllResume = 0, countAllPause = 0;  
  2. this.userdetails.forEach(x => {  
  3. if (type === 1) {  
  4. x.userProctorSignal = 1;  
  5. x.extensionTime = time;  
  6. else if (type === 0) {  
  7. x.userProctorSignal = 0;  
  8. x.extensionTime = 0;  
  9. x.xextensionTime = 0;  
  10. }  
  11. });  
  12. this.xUserDetails.forEach((el, indx) => {  
  13. if (type === 1 && el.userProctorSignal === 1) {  
  14. el.userProctorSignal = type;  
  15. el.extensionTime = time;  
  16. el.xextensionTime = time;  
  17. countAllPause++;  
  18. }  
  19. if (type === 0 && el.userProctorSignal === 0) {  
  20. el.userProctorSignal = 0;  
  21. el.extensionTime = 0;  
  22. el.xextensionTime = 0;  
  23. countAllResume++;  
  24. }  
  25. })  
  26. if (this.xUserDetails.length === countAllPause) {  
  27. this.timeForallUser = time;  
  28. this.toggleButtonDisplayType = 1;  
  29. else if (this.xUserDetails.length === countAllResume) {  
  30. this.timeForallUser = 0;  
  31. this.toggleButtonDisplayType = 0;  
  32. }  
IV Code:
  1. this.userdetails.forEach((el, indx) => {  
  2. if (el.examUserId === examUserDetail.examUserId) {  
  3. this.userdetails[indx] = examUserDetail;  
  4. el.xextensionTime = time;  
  5. el.extensionTime = time;  
  6. }  
  7. });  
V Code:
  1. if (this.userdetails[i].extensionTime !== null && this.userdetails[i].extensionTime > 0) {  
  2. this.userdetails[i].extensionTime /= 60;  
  3. this.userdetails[i]['xextensionTime'] = this.userdetails[i].extensionTime;  
  4. else if (this.userdetails[i].extensionTime === null || this.userdetails[i].extensionTime === 0) {  
  5. this.userdetails[i].extensionTime = 0;  
  6. this.userdetails[i]['xextensionTime'] = 0;  
  7. }  
The above 5 coding snippets were written in a old school manner with vanilla JS. As I require further assistance of converting this into advanced & much smaller lines of code that would suffice a lot.
Any help with single or partial snippets which could be simplified would be great and are always welcome to post..

Answers (2)