1

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?

4 Answers 4

3

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

if (value[key] === '') {
  delete value[key];
}
Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

0

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);
  }

Comments

0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.