I have this array of objects shown below
Object {Results:Array[3]}
Results:Array[3]
[0-2]
0:Object
id=null
name: "Rick"
Value: "34343"
1:Object
id=2
name: null
Value: "2332"
2:Object
id=2
name:'mike'
Value: null
As you can see, in 1 object i have id as null, 2nd object has name as null and 3rd object has Value as null. Each object has some property as null.
I want to loop through all of these and replace null with ''. Can someone let me know how to achieve this...
Here's something quick:
var results = [{
id: null,
name: "Rick",
Value: "34343"
}, {
id: 2,
name: null,
Value: "2332"
}, {
id: 2,
name: 'mike',
Value: null
}];
results.forEach(function(object){
for(key in object) {
if(object[key] == null)
object[key] = '';
}
});
console.log(JSON.stringify(results, null, 2))
You only needed to Google looping through objects. Here's an example:
for (const obj of arr) {
if (typeof obj !=== 'object') continue;
for (k in obj) {
if (!obj.hasOwnProperty(k)) continue;
v = obj[k];
if (v === null || v === undefined) {
obj[k] = '';
}
}
}
for (const obj of arr) {
if (obj.name === undefined || obj.name === null) {
obj.name = '';
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With