how to get key and value of object whose values are not equal to null?

Biraj Gautam

I have an object that looks like this:

let data = {
      toDate: this.state.toDate,
      fromDate: this.state.fromDate,
      filteredEntityText: null,
      options: "negative",
      searchTerm: null
    };

From the above object I want to generate a new object that only has key value pairs whose values are not null.

The expected result should look like this:

let newData = {
    toDate: this.state.toDate,
    fromDate: this.state.fromDate,
    options: "negative"
};

Here I have used a static example of data object, the null values may differ later. Basically I want a new object with key value pairs whose value is not null. Can anyone help me with a solution?

ATUL SHARMA

This all will help you to remove nested null items as well. Using some ES6 / ES2015: Below some example will modify data object directly or if you want to create duplicate object with removed nulls you can call functions given below it will return new object.

If you don't like to create an extra function and remove the items 'inline'.

Object.keys(data).forEach(k => (!data[k] && data[k] !== undefined) && delete data[k]);

Same, written as a function.

const removeEmpty = (data) => {
  Object.keys(data).forEach((k) => (!data[k] && data[k] !== undefined) && delete 
       data[k]);
  return data;
};

This function uses recursion to delete items from nested objects as well:

const removeEmpty = (data) => {
  Object.keys(data).forEach(k =>
    (data[k] && typeof data[k] === 'object') && removeEmpty(data[k]) ||
    (!data[k] && data[k] !== undefined) && delete data[k]
  );
  return data;
};

Same as function before but with ES7 / 2016 Object.entries:

const removeEmpty = data => {
  Object.keys(data).forEach(
    k => !data[k] && data[k] !== undefined && delete data[k]
  );
  return data;
};

Same as third example but in plain ES5:

function removeEmpty(data) {
  Object.keys(data).forEach(function(key) {
    (data[key] && typeof data[key] === 'object') && removeEmpty(data[key]) ||
    (data[key] === '' || data[key] === null) && delete data[key]
  });
  return data;
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get a key in a JavaScript object by its value?

How to list the values of a json object that are equal to key in an array?

JavaScript -- how to iterate through an array of objects to create a new object whose key is the value of the original object's initial key/value pair

How to get a object's value by a key list

How to get a key/value pair for an object

How to get an object value, which property is equal to a variable value

How to get the object key and value into an array in JS?

How to access value of a JSON object whose key is value of another JSON object?

How to get the object key it has the value

Get all products with specific meta_key whose meta_value is equal in WooCommerce

PySpark how to sort by a value, if the values are equal sort by the key?

How to get column name whose value is not null in mysql

how to get object by key and value

Get object where the value of key is equal to array value

How to get JSON object values for a Particular key

How to Get a Key value from a dictionary whose values are lists?

How to get key and value of object in javascript?

How do I check if the value attribute is equal object key, if its equal take the key value and add a class to it

How to get a key from Object values?

How to get the key for a array with his value is equal True

How to add a new key and value in null object?

How to validate nested object whose keys should match with outer objects another key whose value is array using Joi?

How to check if the value of a key is equal through out the list of JSON Object

How to get key values in a json object(Python)

How to get the key and value from an object in typescript

How to merge the multiple object if the key value equal

Typescript: How to declare a type of array whose values are equal to keys of object?

How to get Json key name if its value is equal to "x" - Python

An object whose values are arrays, whose elements may be objects. How to get the key of a value as deep as possible?