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

157
Views
How can I make redundant and unnecessary sentences shorthand in javscript?

How can I make redundant and unnecessary sentences shorthand in javscript?

My code is below.

    //initialization
    document.querySelector('#categoryCheckNormal').checked = false;
    document.querySelector('#categoryCheckDisabled').checked = false;
    document.querySelector('#categoryCheckBlind').checked = false;
    document.querySelector('#categoryCheckEvacuation').checked = false;
    // If there is a classification, put a value
    if (res.eventData.property.normal) {
        document.querySelector('#categoryCheckNormal').checked = true;
    } else if (res.eventData.property.disabled) {
        document.querySelector('#categoryCheckDisabled').checked = true;
    } else if (res.eventData.property.blind) {
        document.querySelector('#categoryCheckBlind').checked = true;
    } else if (res.eventData.property.evacuation) {
        document.querySelector('#categoryCheckEvacuation').checked = true;
    }

By the way, the code is redundant and less readable.

I'm wondering how I can change this as a best practice!

Best Regards!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I would probably approach it with the following method.

  1. Add data attributes to the input boxes that match your classifications.
  2. Grab all the inputs at once with querySelectorAll.
  3. You can then have you reset function iterate over them and reset the checked status.
  4. You can then grab the classifications from the property object, iterate over them, and then for each create a selector from the property key, query that element, and set the checked status.

// Get all the inputs
const inputs = document.querySelectorAll('input[type="checkbox"]');

// Iterate over them and reset them
function reset() {
  inputs.forEach(input => input.checked = true);
}

reset();

// Get some data
const res={eventData:{property:{normal:true,disabled:false,blind:false,evacuation:true}}};

// Get the property object
const { property } = res.eventData;

// Get its entries
const entries = Object.entries(property);

// Finally iterate over those entries and update
// the inputs based the key of each entry
for (const [key, value] of entries) {
  const selector = `[data-id="${key}"]`;
  const input = document.querySelector(selector);
  input.checked = value;
}
Normal: <input type="checkbox" data-id="normal">
Disabled: <input type="checkbox" data-id="disabled">
Blind: <input type="checkbox" data-id="blind">
Evacuation: <input type="checkbox" data-id="evacuation">

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!