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.
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()