Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

73
Views
trying to minimalize the js code and want to increase the performance speed in js

I'm trying to avoid to write the number of lines since the response is same for all the fields so I tried to use for loop with categorizing the fields['mandatory', 'optional'] but still I need to write twice time for loop. Is this possible to write more efficiently without compromising the performance in one go.

    //mandatory
obj.required['test1' + number] = true;
obj.visible['test1' + number] = true;
obj.required['test2' + number] = true;
obj.visible['test2' + number] = true;
obj.required['test3' + number] = true;
obj.visible['test3' + number] = true;
//optional
obj.visible['test4' + number] = true;
obj.visible['test5' + number] = true;

Tried with for loop:

var mandatoryField = ['test1', 'test2', 'test3'];
var optionalField = ['test4', 'test5'];

for(var i=0; i<mandatoryField.length; i++) {
obj.required[madatoryField[i] + number] = true;
obj.visible[madatoryField[i] + number] = true;
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Something like this wll help you to achieve this in one iteration.

var mandatoryField = ['test1', 'test2', 'test3'];
var optionalField = ['test4', 'test5'];

const limit = new Array(Math.max(mandatoryField.length, optionalField.length)).fill(0);
limit.forEach((item, index) => {
    if(mandatoryField[index]) {
        obj.required[mandatoryField[index] + number] = true;
        obj.visible[mandatoryField[index] + number] = true;
    }
    if(optionalField[index]) {
        obj.visible[optionalField[index] + number] = true;
    }
})

about 4 years ago · Juan Pablo Isaza Report

0

Can you change the structure of the fields?

const obj = {
  visible: {
    test1: false,
    test2: false,
    test3: false,
    test4: false,
    test5: false,
    test6: false,
    test7: false,
    test8: false,
  },
  required: {
    test1: false,
    test2: false,
    test3: false,
    test4: false,
    test5: false,
    test6: false,
    test7: false,
    test8: false,
  },
};
const fields = [
  { field: 'test1', required: true },
  { field: 'test2', required: true },
  { field: 'test3', required: true },
  { field: 'test4' },
  { field: 'test5' },
];

fields.forEach(({ field, required }) => {
  const key = field; // `${field}${number}`;

  obj.visible[key] = true;

  if (required) {
    obj.required[key] = true;
  }
});

console.log(obj);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!