0

I have Javascript function and I want to check multiple if statements in return.

If the value exist or is not null then it should return a row with information,

I am not sure how to do this, I am thinking something like this, E.g:

function format(d){
    return '<table cellpadding="5" cellspacing="0" border="0">' +
    if (name) {
     '<tr>' +
         '<td>Name:</td>' +
         '<td>' + d.name + '</td>' +
     '</tr>' } +
     if (place)
     '<tr>' +
         '<td>Place:</td>' +
         '<td>' + d.place + '</td>' +
     '</tr>' } +
     if (date) {
     '<tr>' +
         '<td>Date:</td>' +
         '<td>' + d.date + '</td>' +
     '</tr>' } +
    '</table>'; 
}
2
  • 2
    you can't do that with if - its not an expression - you could youse ternary but thats messy - consider using building the string on your way out Commented Feb 5, 2021 at 0:35
  • You mean like this? jsfiddle.net/#&togetherjs=IErTskSkPt Commented Feb 5, 2021 at 0:46

3 Answers 3

2

Like Daniel pointed out, you could build the string on your way out like this:

function  myFormat(d) {
      let result = '<table cellpadding="5" cellspacing="0" border="0">';
      if (name){
        result += '<tr>' + '<td>Name:</td>' + '<td>' + d.name + '</td>' + '</tr>';
      }
      if (place){
        result += '<tr>' + '<td>Place:</td>' + '<td>' + d.place + '</td>' + '</tr>'; 
      }
      if (date){
        result += '<tr>' + '<td>Date:</td>' + '<td>' + d.date + '</td>' + '</tr>'; 
      }
      result += '</table>';
      return result;
}

Html elements could be combined together as well to make it simpler like this:

'<tr><td>Place:</td><td>' + d.name + '</td></tr>'
Sign up to request clarification or add additional context in comments.

Comments

0

I see that you can create a template for row. Why not to use a separate function:

formatRow(value, label)

'formatRow' function can use if statement to check if value is empty and decide to return a row or empty string for concatenation.

function formatRow(value, label){
  if(value) {
    return '<tr>' +
         '<td>' + label + '</td>' +
         '<td>' + value + '</td>' +
     '</tr>';
  }
  return '';
}

Then run it within format() function for each property that you want to use.

Comments

0

function createCell(text){
  const td = document.createElement('td');
  td.textContent=text;
  return td
}

function createRow(data, key){
  row = document.createElement('tr');
  row.appendChild(createCell(key[0].toUpperCase() + key.substring(1) + ':' ))
  row.appendChild(createCell(data[key]))
  return row; 
}

function format(d){
  table = document.createElement('table');
  table.setAttribute("cellpadding", "5");
  table.setAttribute("border", "0");
  table.setAttribute("cellspacing", "0");
  Object.keys(d).forEach(key => table.appendChild(createRow(d, key)))
  return table
}

document.body.appendChild(format({data: '5 feb 2021', name: 'John', place: 'New York' }))   
document.write('<hr>')
document.body.appendChild(format({data: '5 feb 2021', name: 'John' }))   
document.write('<hr>')
document.body.appendChild(format({data: '5 feb 2021' }))   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.