I am facing an issue where I need to remove the 'hello' substring from each object field in my objects array. However, I keep getting an error message saying "Cannot read property 'indexOf' of null". This error is occurring because I am attempting to modify an object field within a loop. Any suggestions on how to resolve this? :) I am using AngularJS.
var array = [
{text: 'hello user1'},
{text: 'hello user2'},
{text: 'user3'},
{text: 'hello user4'},
];
for (i = 0; i < array.length; i++) {
if (array[i].text.indexOf('hello') + 1) {
array[i].text = array[i].text.replace('hello','');
}
}
// For demo
document.write(JSON.stringify(array));