khansen
khansen

Reputation: 205

How can remove keys with empty values in an Object Array in TypeScript?

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?

Upvotes: 1

Views: 3049

Answers (4)

Saurabh Yadav
Saurabh Yadav

Reputation: 3386

var list = [{
        "flightID": "FooID",
        "direction": "DownFoo",
        "msgType": "FooType",
        "elemNb": "",
        "msgID": "",
    },
    {
        "flightID": "FooID2",
        "direction": "UpFoo",
        "msgType": "FooType2",
        "elemNb": "",
        "msgID": "",
}];

let result = Array.from(list, o=> Object.fromEntries(Object.entries(o).filter((i) => i[1] != (null || ''))));

console.log(result);

var list = [{
        "flightID": "FooID",
        "direction": "DownFoo",
        "msgType": "FooType",
        "elemNb": "",
        "msgID": "",
    },
    {
        "flightID": "FooID2",
        "direction": "UpFoo",
        "msgType": "FooType2",
        "elemNb": "",
        "msgID": "",
}];

let result = Array.from(list, o=> Object.fromEntries(Object.entries(o).filter((i) => i[1] != (null || ''))));

console.log(result);

Upvotes: 2

Ramesh Thiruchelvam
Ramesh Thiruchelvam

Reputation: 346

You've to check if the value is empty and remove the key. They the below code.

myList: any[]

removeEmptyValues() {
  if (this.myList) {

    this.myList.forEach((value) => {

      Object.keys(value).forEach((key) => {
        if(!value[key]) delete value[key];
      })
    })
  }
  console.log(this.myList);
}

Upvotes: 0

Michał Tkaczyk
Michał Tkaczyk

Reputation: 736

Try deleting value[key] instead of key['']

if (value[key] === '') {
  delete value[key];
}

Upvotes: 3

Amit Rai
Amit Rai

Reputation: 397

You have to remove the key from object

myList: any[]

 removeEmptyValues() {
    if (this.myList) {

      this.myList.forEach((value) => {

        Object.keys(value).forEach((key) => {
          delete this.myList[key]

        })
      })
    }
    console.log(this.myList);
  }

Upvotes: 0

Related Questions