Skip to content Skip to sidebar Skip to footer

Can This Be Turned Into A For Loop?

I would like to clean this up with a For loop. What would be the most efficient way coding this out? I'm creating a search form that looks through a database for the specific form

Solution 1:

Sure.

var html = "";
obj.Fields.forEach(({DisplayName, DataValue}) => {
  html += `<p>${DisplayName}: ${DataValue}</p>`;
});
document.getElementById("test").innerHtml = html;

Solution 2:

Use Array.map() and join the results:

var fields = data[0].Fields ;

document.getElementById("test").innerHTML = fields
  .map(function(field) {
    return'<p>' + field.DisplayName + ': ' + field.DataValue + '</p>';
  })
  .join('\n');

Post a Comment for "Can This Be Turned Into A For Loop?"