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

206
Views
How can i loop over an array to get jquery properties?

I have some fields that I want to hide if they are null. I am able to do this with:

if (json[0].incendio_edificio == null) {
  $("#dIncendio_edificio").parent().parent().hide();
}

if (json[0].hvct_edificio == null) {
  $("#dHvct_edificio").parent().parent().hide();
}

if (json[0].granizo_edificio == null) {
  $("#dGranizo_edificio").parent().parent().hide();
}

What I want to do is put the fields inside a matrix and do the conditional inside a loop, like this:

const campos_poliza = [
                         [json[0].incendio_edificio, $("#dIncendio_edificio")],
                         [json[0].hvct_edificio, $("#dHvct_edificio")],
                         [json[0].granizo_edificio, $("#dGranizo_edificio")],
                      ];

for (var x in campos_poliza){
  if (x[0] == null) {
    x[1].parent().parent().hide();
  }
}

This doesn´t give any errors but it keeps showing null fields. How can I achieve this? The html is:

<div class="row">
  <div class="col s3">
    <span class="text-bold text-blue">Incendio Edificio:</span>
  </div>
  <div class="col s9">
    <span id="dIncendio_edificio"></span>
  </div>
</div>
<div class="row">
  <div class="col s3">
    <span class="text-bold text-blue">HVCT Edificio:</span>
  </div>
  <div class="col s9">
    <span id="dHvct_edificio"></span>
  </div>
</div>
<div class="row">
  <div class="col s3">
    <span class="text-bold text-blue">Granizo Edificio:</span>
  </div>
  <div class="col s9">
    <span id="dGranizo_edificio"></span>
  </div>
</div>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

When you use for (var x in campos_poliza) , x is the index of array, which is 0, 1, 2...

This might be what you need:

for (var x of campos_poliza) {
  if (x[0] == null) {
    x[1].parent().parent().hide();
  }
}

Or

campos_poliza.forEach(x => {
  if (x[0] == null) {
    x[1].parent().parent().hide();
  }
});
about 4 years ago · Juan Pablo Isaza Report

0

Why not toggle if existing

const obj = [{ incendio_edificio:null, hvct_edificio : "not null", granizo_edificio : null }]
Object.entries(obj[0]).forEach(([key,val]) => { 
  const sel = `#d${key.slice(0,1).toUpperCase()}${key.slice(1)}`; 
  console.log(key,val,sel)
  const $elem = $(sel)
  if ($elem) $elem.closest(".row").toggle(val!=null)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="row">
  <div class="col s3">
    <span class="text-bold text-blue">Incendio Edificio:</span>
  </div>
  <div class="col s9">
    <span id="dIncendio_edificio"></span>
  </div>
</div>
<div class="row">
  <div class="col s3">
    <span class="text-bold text-blue">HVCT Edificio:</span>
  </div>
  <div class="col s9">
    <span id="dHvct_edificio"></span>
  </div>
</div>
<div class="row">
  <div class="col s3">
    <span class="text-bold text-blue">Granizo Edificio:</span>
  </div>
  <div class="col s9">
    <span id="dGranizo_edificio"></span>
  </div>
</div>

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!