I have an array of data. What I want to do is to delete the attribute from the objects where it is null, like in case of the 3rd object I want to delete the img attribute. What I have tried so far is
var data = [{
id: 1,
name: 'name1',
img: 'car1'
},
{
id: 2,
name: 'name2',
img: 'car2'
},
{
id: 3,
name: 'name3',
img: null
}
]
data.forEach(function(value) {
for (var key in value) {
if (value.hasOwnProperty(key)) {
var val = value[key];
if (val == null) {
delete data[value[key]];
}
}
}
});
console.log(data);
But the deletion is not happening, any idea what am I doing wrong?