How to Remove Empty String Values from an Object in JavaScript?

Estimated read time 1 min read

To remove empty string values from an object in JavaScript, you can use the Object.entries() and filter() methods. Here’s an example:

let obj = {
  key1: "",
  key2: "value2",
  key3: "",
  key4: "value4"
};

let newObj = Object.fromEntries(
  Object.entries(obj).filter(([, value]) => value !== "")
);
console.log(newObj);

This code removes the properties with empty string values from the object {key1: "", key2: "value2", key3: "", key4: "value4"} and logs the modified object: {key2: "value2", key4: "value4"}.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply