I'm trying to write a function in my Angular 8 project. I have an Array of Objects and want to remove all key:value pairs where the value is empty. There are many examples on the internet but none of them seem to work for me.
What I have:
{
"flightID": "FooID",
"direction": "DownFoo",
"msgType": "FooType",
"elemNb": "",
"msgID": "",
},
{
"flightID": "FooID2",
"direction": "UpFoo",
"msgType": "FooType2",
"elemNb": "",
"msgID": "",
},
What I want:
{
"flightID": "FooID",
"direction": "DownFoo",
"msgType": "FooType",
},
{
"flightID": "FooID2",
"direction": "UpFoo",
"msgType": "FooType2",
},
My attempt:
myList: any[]
removeEmptyValues() {
if (this.myList) {
this.myList.forEach((value) => {
Object.keys(value).forEach((key) => {
delete key[''];
})
})
}
console.log(this.myList);
}
I would expect that delete key[''] would delete the key if the value is empty but it isn't doing anything. How can I delete the key if the value is empty?