TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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
let pauseAllcount = 0, resumeAllcount = 0;
this
.xUserDetails.forEach((el, indx) => {
if
(
this
.xUserDetails[indx].userProctorSignal === 1) {
pauseAllcount++;
}
else
if
(
this
.xUserDetails[indx].userProctorSignal === 0) {
resumeAllcount++;
}
});
The above snippets could be written as
let pauseAllcount = 0, resumeAllcount = 0;
pauseAllcount =
this
.xUserDetails.filter(x => x.userProctorSignal === 1).length;
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:
if
(
this
.pageReqDetails.RowTot ===
this
.xUserDetails.length) {
if
(
this
.userdetails.every(x => x.extensionTime ===
this
.userdetails[0].extensionTime)) {
this
.timeForallUser =
this
.userdetails[0].extensionTime;
}
if
(pauseAllcount ===
this
.xUserDetails.length) {
this
.toggleButtonDisplayType = 1;
}
else
if
(resumeAllcount ===
this
.xUserDetails.length) {
this
.toggleButtonDisplayType = 0;
this
.timeForallUser = 0;
}
else
{
this
.toggleButtonDisplayType = -1;
this
.timeForallUser = 0;
}
}
else
{
this
.timeForallUser = 0;
}
II Code:
let flagPauseAll =
false
, flagResumeAll =
false
;
if
(
this
.pageReqDetails.RowTot ===
this
.xUserDetails.length) {
this
.xUserDetails.forEach((el, indx) => {
if
(type === 0) {
if
(el.extensionTime === time) {
this
.timeForallUser = 0;
this
.toggleButtonDisplayType = 0;
}
else
{
flagResumeAll =
true
;
}
}
else
if
(type === 1) {
if
(el.extensionTime === time) {
this
.timeForallUser = time;
this
.toggleButtonDisplayType = 1;
}
else
{
flagPauseAll =
true
;
}
}
});
}
if
(flagPauseAll || flagResumeAll) {
this
.timeForallUser = 0;
this
.toggleButtonDisplayType = -1;
}
III Code:
let countAllResume = 0, countAllPause = 0;
this
.userdetails.forEach(x => {
if
(type === 1) {
x.userProctorSignal = 1;
x.extensionTime = time;
}
else
if
(type === 0) {
x.userProctorSignal = 0;
x.extensionTime = 0;
x.xextensionTime = 0;
}
});
this
.xUserDetails.forEach((el, indx) => {
if
(type === 1 && el.userProctorSignal === 1) {
el.userProctorSignal = type;
el.extensionTime = time;
el.xextensionTime = time;
countAllPause++;
}
if
(type === 0 && el.userProctorSignal === 0) {
el.userProctorSignal = 0;
el.extensionTime = 0;
el.xextensionTime = 0;
countAllResume++;
}
})
if
(
this
.xUserDetails.length === countAllPause) {
this
.timeForallUser = time;
this
.toggleButtonDisplayType = 1;
}
else
if
(
this
.xUserDetails.length === countAllResume) {
this
.timeForallUser = 0;
this
.toggleButtonDisplayType = 0;
}
IV Code:
this
.userdetails.forEach((el, indx) => {
if
(el.examUserId === examUserDetail.examUserId) {
this
.userdetails[indx] = examUserDetail;
el.xextensionTime = time;
el.extensionTime = time;
}
});
V Code:
if
(
this
.userdetails[i].extensionTime !==
null
&&
this
.userdetails[i].extensionTime > 0) {
this
.userdetails[i].extensionTime /= 60;
this
.userdetails[i][
'xextensionTime'
] =
this
.userdetails[i].extensionTime;
}
else
if
(
this
.userdetails[i].extensionTime ===
null
||
this
.userdetails[i].extensionTime === 0) {
this
.userdetails[i].extensionTime = 0;
this
.userdetails[i][
'xextensionTime'
] = 0;
}
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..
Reply
Answers (
2
)
convert string to boolean in angular typescript
Appending Dynamic variable value to ng model of mat select is not work