I'm currently working with an array of data and my goal is to remove any attributes from the objects where they are null. For example, I want to delete the img attribute from the 3rd object. Here is the approach I have taken so far:
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);
However, I am facing an issue with the deletion process not happening as expected. Can anyone provide insights on what might be going wrong?