1

I was trying to replace the null value that I get through a JSON after submitting a form by a String value like 'n/a' or 'not specified'. I have added few lines as @Winter Solider suggested which is commented below to check null value and replace it but the code I added doesn't work. And idea?
-thanks

function submitFormFunction() {
  //document.getElementById("form").submit();
  var valueArray = [
    {
      'label': 'contractId',
      'value': document.getElementById('ContractId').value
    },
    {
      'label': 'title',
      'value': document.getElementById('ContractTitle').value
    },
    {
      'label': 'minYear',
      'value': document.getElementById('MinYear').value
    },
    {
      'label': 'maxYear',
      'value': document.getElementById('MaxYear').value
    },
    {
      'label': 'terminal',
      'value': document.getElementById('Terminal').value
    },
    {
      'label': 'location',
      'value': document.getElementById('Location').value
    },
    {
      'label': 'theme',
      'value': document.getElementById('Theme').value
    },
    {
      'label': 'construction',
      'value': document.getElementById('Construction').value
    },
    {
      'label': 'asBuilt',
      'value': document.getElementById('AsBuilt').value
    }
  ].map(function (param) { return param.label + '=' + param.value; });
        
  if (valueArray.length) {
    // here I am trying to handle the null value issue as suggested by Winter Soldier 
    for (var i = 0; i < valueArray.length; i++) {
      if (valueArray[i].includes("null")) {
        valueArray[i] = valueArray[i].replace("null", "n/a");
      }
    }
  
    console.log(valueArray)
    console.log(valueArray.join('&'));
    //var queryStr = JSON.stringify(valueArray.replacer);
    var queryString = valueArray.join('&');
    fetch(searchUrl, '?' + queryString, function (data) {
      // output search results to the dom
      renderSearchResults(JSON.parse(data), document.getElementById('searchResults'));
    });
  } else {
      document.getElementById('searchResults').innerHTML = "Please enter a search term.";
  }
}

4 Answers 4

4

Observation: You are using filter unnecessarily as it creates another array on valueArray array. Instead you can use forEach. and

Solution:

if (valueArray.length) { // It'll do the same job as 'valueArraylength > 0'
    // here I am trying to handle the null value issue
    valueArray.forEach(function(value){
        if (value == null || value == "") {
          value = "n/a";
        }
    })
    //var queryStr = JSON.stringify(valueArray.replacer);
    var queryString = valueArray.join('&');
    fetch(searchUrl, '?' + queryString, function (data) {
        // output search results to the dom
        renderSearchResults(JSON.parse(data), document.getElementById('searchResults'));
    });
}

Hope this helps :)

Sign up to request clarification or add additional context in comments.

Comments

1
  • Your null values are getting filtered out even before you execute the map. Whenever there is a null value from element you are looking up, it ends up being filtered out.
  • So you are better off not using the filter. Modify your code based on other answers. Switch to each or something.
  • Though I have 9 inputs, it only prints 8

See the below code

var valueArray = [
  {
    'label': 'contractId',
    'value': 'ContractId'
  },
  {
    'label': 'title',
    'value': 'ContractTitle'
  },
  {
    'label': 'minYear',
    'value': 'MinYear'
  },
  {
    'label': 'maxYear',
    'value': 'MaxYear'
  },
  {
    'label': 'terminal',
    'value': 'Terminal'
  },
  {
    'label': 'location',
    'value': 'Location'
  },
  {
    'label': 'theme',
    'value': 'Theme'
  },
  {
    'label': 'construction',
    'value': 'Construction'
  },
  {
    'label': 'asBuilt',
    'value': null
  }
].filter(function (param) { return param.value; })

console.log(valueArray)
console.log(valueArray.length)

EDIT:

  • Is this what you need at the end of null check?
  • If you are trying to replace the null string value with n/a, this is perhaps what you'll need to do
  • Edited the code to refelct "" check

var valueArray = [{
  'label': 'contractId',
  'value': 'ContractId'
}, {
  'label': 'title',
  'value': 'ContractTitle'
}, {
  'label': 'minYear',
  'value': 'MinYear'
}, {
  'label': 'maxYear',
  'value': 'MaxYear'
}, {
  'label': 'terminal',
  'value': 'Terminal'
}, {
  'label': 'location',
  'value': 'Location'
}, {
  'label': 'theme',
  'value': ''
}, {
  'label': 'construction',
  'value': 'Construction'
}, {
  'label': 'asBuilt',
  'value': null
}].map(function(param) {
  return param.label + '=' + param.value;
});
if (valueArray.length) {
  // here I am trying to handle the null value issue
  for (var i = 0; i < valueArray.length; i++) {
    if (valueArray[i].includes("null") || !valueArray[i].split("=")[1].length ) {
      valueArray[i] = valueArray[i].split("=")[0] + "=n/a";
    }
  }
  console.log(valueArray)
  console.log(valueArray.join('&'));
  //the rest of your code
}

20 Comments

thanks for your comment but im confused. if its being filtered how i get still null values when i populate a table with the searchResult output?
Check if your document.getElementById('XYZ').value is returning a null object or 'null' string
how can i check that? ^_^
By running for example this document.getElementById('ContractId').value in developer window's console of your browser
@alast There you go. Please accept the answer, if it solves your question.
|
1

It works correctly for me. Perhaps you need to check for the empty string as well?

var valueArray=[5,6,7,8,null,3,4,5,0,0,0,9,''," "]
            for (var i = 0; i < valueArray.length; i++) {
                if (valueArray[i] === null || valueArray[i] === ' ' || valueArray[i] === '') {
                    valueArray[i] = 'n/a';
                }
              console.log(valueArray[i]);
            }

3 Comments

I issue was with the filter
Ah, hope you found a solution :)
You saved me my weekend! God bless you!
0

var valueArray = [{
  'label': 'contractId',
  'value': 'ContractId'
}, {
  'label': 'title',
  'value': 'ContractTitle'
}, {
  'label': 'minYear',
  'value': 'MinYear'
}, {
  'label': 'maxYear',
  'value': 'MaxYear'
}, {
  'label': 'terminal',
  'value': 'Terminal'
}, {
  'label': 'location',
  'value': 'Location'
}, {
  'label': 'theme',
  'value': ''
}, {
  'label': 'construction',
  'value': 'Construction'
}, {
  'label': 'asBuilt',
  'value': null
}];
for (var i = 0; i < valueArray.length; i++) {
                            Object.keys(valueArray[i]).forEach(function (field) {
                            if(valueArray[i][field] == null){
                            valueArray[i][field] = "";
                            }
                            })                   
                    };
                    console.log(valueArray);

3 Comments

Hope This suits your question
Can you explain how the code works in the answer? Thanks!
I'm looping the whole array and again fetches each object keys. So that instead giving a static key value I'm getting it dynamically. Using this way I'm checking each and every value of an objects.

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.