After submitting a form and receiving null values through JSON, I wanted to replace them with String values like 'n/a' or 'not specified'. Even though I followed @Winter Soldier's suggestion and added code to check for null values and replace them, it doesn't seem to be working. Any thoughts on how to fix this?
-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) {
// attempt 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.";
}
}