How to use Object.values() with Array.prototype.includes() In Javascript

In this approach, we are using Object.values() to extract an array of values from the JSON object, and then we use Array.prototype.includes() to check if the array includes null, indicating the presence of a null value in the JSON object.

Syntax:

const valuesArray = Object.values(object);
if (valuesArray.includes(value)) {
// code
}

Example: The below uses Object.values() with Array.prototype.includes() to check if the json key value is null in JavaScript.

JavaScript
const jsonData = {
    key1: null,
    key2: "value2",
    key3: undefined,
};
const res = Object.values(jsonData);
if (res.includes(null)) {
    console.log(
        "Null value is present in the JSON object.");
} else {
    console.log(
        "No null value found in the JSON object.");
}

Output
Null value is present in the JSON object.

How to Check if JSON Key Value is Null in JavaScript ?

In JavaScript, the JSON Object can contain null values, which should be identified. The below approaches can be used to check if the JSON key is null.

Table of Content

  • Using for Loop and ‘===’ Operator
  • Using Object.values() with Array.prototype.includes()
  • Using Array.prototype.some()

Similar Reads

Using for Loop and ‘===’ Operator

In this approach, we are using a for loop to iterate through each key in the JSON object and checking if its corresponding value is null using the strict equality operator ===, setting a flag to true if a null value is found....

Using Object.values() with Array.prototype.includes()

In this approach, we are using Object.values() to extract an array of values from the JSON object, and then we use Array.prototype.includes() to check if the array includes null, indicating the presence of a null value in the JSON object....

Using Array.prototype.some()

In this approach, we are using Object.values() to extract an array of values from the JSON object, and then we use Array.prototype.some() with a callback function to check if any value in the array is null, indicating the presence of a null value in the JSON object....

Contact Us