Javascript check for null property

How to check all object property is null JavaScript

In this post we are going to learn How to check all object property is null in JavaScript by using some function of javascript. The JavaScript array. every() and array.some() functions are used to check for null values, We will get all the keys with null empty, and undefined values from an object.

1. How to check all object property is null JavaScript

The array. every() method calls a function for every element of the array. If all the elements satisfy the condition set by the function then an array.every() will return True else return False if any element of the array does not satisfy the condition. For an empty array, this function always returns False.

  • To check all object properties is a null array, the First we will call object. values() to get an array of all values of an object.
  • The array. every() method will run a function to check if each element is ‘Null’
  • if all the values are ‘Null’ then an array.every() method will return True
let Employee = const isObjnull = Object.values(Employee).every(value => < if (value === null) < return true; >return false; >); console.log(isObjnull)

3. How to check all object property is null or empty

The Javascript array.some() function check if one of array element satisfying the given condition set by array.some() function then return true else return False .In this javascript program the array.some() function is if the object property is empty and not null.

let Employee = const isObjnull = !Object.values(Employee).some(val => (val !== null && val !== ' ')); console.log(isObjnull)

4. Get key object is null or empty

Sometimes we have to need to get all the keys of objects that have undefined, null, and empty values, First, we will get all object keys as an array using the object. keys().

  • The array. filter() method will execute a callback function for each element of the array to check for a condition where values are undefined, null, or empty.
  • It returns an array of keys that satisfies the condition this is how we get if the keys have undefined, null, empty values.
let Employee = const keys= Object.keys(Employee).filter((key)=> < if (EmployeeJavascript check for null property === "" || EmployeeJavascript check for null property===undefined || EmployeeJavascript check for null property===null) < return key >>); console.log(keys)
[ 'Empname', 'salary', 'EmpID', 'Dep', 'EmpProj' ]

5. Function to check all object property is null JavaScript

In this Javascript program example, we defined a function to check if the values of objects are null or empty. We have defined a boolean variable and initialed it to True.They are checking if EmployeeJavascript check for null property === null || EmployeeJavascript check for null property === “” using the strict equality operator.Let us understand with the below program

let Employee = function checkforNull(Employee) < var isnul = true; for (var key in Employee) < if ( !( EmployeeJavascript check for null property === null || EmployeeJavascript check for null property === "" ) ) < isnul = false; break; >> return isnul; > console.log(checkforNull(Employee))

Check object property for Falsy values

The falsy values in javascript ‘false’,’null’,’undefined’,’NaN’.Let us understand how to check for Falsy values in object in JavaScript.

let Employee = const isFVal = Object.values(Employee).every(value => < if (!value) < return true; >return false; >); console.log(isFVal);

Summary

In this post we have learned how to check all object property is null JavaScript with examples by using array.every() and array.some() function and also get all key which has null empty and undefined values

Читайте также:  Jquery get html request

Источник

Javascript check for null property

Last updated: Dec 21, 2022
Reading time · 5 min

banner

# Table of Contents

# Check if all Object Properties are Null

To check if all of an object’s properties have a value of null :

  1. Use the Object.values() method to get an array of the object’s values.
  2. Use the Array.every() method to iterate over the array.
  3. Check if each value is equal to null .
  4. The every() method will return true if all values are null .
Copied!
const obj = a: null, b: null>; const isNullish = Object.values(obj).every(value => if (value === null) return true; > return false; >); console.log(isNullish); // 👉️ true

NOTE: if you just need to check if an object is empty, use the Object.keys() method.

Copied!
const obj = a: null, b: null>; if (Object.keys(obj).length === 0) console.log('The object is empty'); > else console.log('The object is NOT empty'); >

We used the Object.values() method to get an array of the object’s values.

Copied!
const obj = a: null, b: null>; // 👇️ [null, null] console.log(Object.values(obj));

The function we passed to the Array.every method gets called with each value in the array.

If the value is equal to null , the function returns true , otherwise false is returned.

The Array.every() method will return true only if the callback function returns true for all values in the array.

If the every() method returns true , then all of the object’s values are null .

You can also check if the object’s values are set to null , undefined or empty string, or any other value that your use case requires.

Copied!
const obj = a: null, b: undefined, c: ''>; const isNullUndefEmptyStr = Object.values(obj).every(value => // 👇️ check for multiple conditions if (value === null || value === undefined || value === '') return true; > return false; >); console.log(isNullUndefEmptyStr); // 👉️ true

The code sample checks if each value in the object is null , undefined or empty string .

We used the || (OR) operator, which means that only 1 of the 3 conditions has to be satisfied for the if block to run.

# Check if all Object properties are True

The same approach can be used if you need to check if all object properties are True .

Copied!
const obj = first: true, second: true, >; const allTrue = Object.values(obj).every( value => value === true ); console.log(allTrue); // 👉️ true

The allTrue variable stores a true value if all properties in the object are true and false otherwise.

# Check if all Object properties are Falsy

You can also check if all values in an object are falsy .

The falsy values in JavaScript are: null , undefined , false , 0 , «» (empty string), NaN (not a number).

All other values in the language are truthy.

Copied!
const obj = a: null, b: undefined, c: '', d: 0, e: false>; const isFalsy = Object.values(obj).every(value => if (!value) return true; > return false; >); console.log(isFalsy); // 👉️ true

All of the values in the object are falsy, so the test function passes on all iterations.

We used the logical NOT (!) operator to convert each value to a boolean and flip the result.

Here are some examples of using the logical NOT operator.

Copied!
console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'hello'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true

The operator converts each value to its boolean representation and flips it.

If all of the values in the object are falsy, the test function we passed to the every() method would return true on all iterations.

# Check if all values in an object are Truthy in JavaScript

To check if all values in an object are truthy:

  1. Use Object.values() to get an array of the object’s values.
  2. Use the Array.every() method to iterate over the array.
  3. Return each value straight away.
Copied!
const obj = first: 'bobbyhadz.com', second: true, third: 1, >; const allTruthy = Object.values(obj).every(value => value); console.log(allTruthy); // 👉️ true

All of the values in the object are truthy, so the test function passes on all iterations because we return each value directly.

An alternative approach is to use a shorthand method, where we leverage the Boolean() constructor.

Copied!
const obj = first: 'bobbyhadz.com', second: true, third: 1, >; const areTruthyShort = Object.values(obj).every(Boolean); console.log(areTruthyShort); // 👉️ true

The Boolean() constructor converts each element in the array to its boolean representation and returns the result.

This code sample achieves the same result as the previous one, however, is a little more concise and implicit.

# Set all Values in an Object to Null in JavaScript

To set all values in an object to null:

  1. Use the Object.keys() method to get an array of the object’s keys.
  2. Use the forEach() method to iterate over the array.
  3. Set each value in the object to null .
Copied!
const obj = name: 'Bobby', age: 30, country: 'Chile', >; Object.keys(obj).forEach(key => obj[key] = null; >); // 👇️ console.log(obj);

We used the Object.keys method to get an array of the object’s keys.

Copied!
const obj = name: 'Bobby', age: 30, country: 'Chile', >; // 👇️ [ 'name', 'age', 'country' ] console.log(Object.keys(obj));

The next step is to use the Array.forEach method to iterate over the array.

On each iteration, we set the value of the current key of null .

After the last iteration, all of the values in the object will be set to null .

An alternative approach is to not mutate the object, but create a new object using the Array.reduce method.

# Set all Values in an Object to Null using reduce()

This is a three-step process:

  1. Use the Object.keys() method to get an array of the object’s keys.
  2. Use the Array.reduce() method to iterate over the array.
  3. Set each object value to null and return the result.
Copied!
const obj = name: 'Bobby', age: 30, country: 'Chile', >; const newObj = Object.keys(obj).reduce((accumulator, key) => return . accumulator, [key]: null>; >, >); // 👇️ console.log(newObj);

The function we passed to the Array.reduce() method gets called for each element in the keys array.

We set the initial value for the accumulator variable to an empty object.

On each iteration, we use the spread syntax (. ) to unpack the key-value pairs of the accumulated object into a new object, setting the current property to null .

# Set all Values in an Object to Null using Object.fromEntries()

This is a three-step process:

  1. Use the Object.keys() method to get an array of the object’s keys.
  2. Use the Array.map() method to get an array of key-value pairs.
  3. Use the Object.fromEntries() method to create an object with null values from the array.
Copied!
const obj = name: 'Bobby', age: 30, country: 'Chile', >; const newObj = Object.fromEntries(Object.keys(obj).map(key => [key, null])); // 👇️ console.log(newObj);

We called the Array.map() method on the array of keys.

The function we passed to the Array.map() method gets called with each element in the array.

On each iteration, we return an array containing the key and a null value.

Copied!
const obj = name: 'Bobby', age: 30, country: 'Chile', >; // 👇️ [ [ 'name', null ], [ 'age', null ], [ 'country', null ] ] console.log(Object.keys(obj).map(key => [key, null]));

The map() method returns a new array containing the values returned from the callback function.

The last step is to use the Object.fromEntries() method to create an object from the array of key-value pairs.

The Object.fromEntries transforms a list of key-value pairs into an object.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Оцените статью