I have the following loop which I have put together and is working (up to a point):
for (var i = 0; i < dataIn.length; i++) {
if (dataIn.YourName = [] ) {
var modifiedName = dataIn[i].YourName.replace(/ \([\s\S]*?\)/g, '');
dataIn[i].YourName = modifiedName;
}
else {
console.log('Do Nothing');
}
}
However, I run into an error when the content from the API source is empty (null). Console error: Cannot read property 'replace' of null
As a result I have tried to update my loop to only run if the information is NOT null or undefined (see below), but it is being ignored and still trying to run.
for (var i = 0; i < dataIn.length; i++) {
if (dataIn.YourName !== null && dataIn.YourName !== '' ) {
var modifiedName = dataIn[i].YourName.replace(/ \([\s\S]*?\)/g, '');
dataIn[i].YourName = modifiedName;
}
else {
console.log('Do Nothing');
}
}
Sample data from the API.
What works:
"YourName": "John Citizen",
What throws the console error:
"YourName": null,