How to use Array.prototype.some() 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.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.
Syntax:
const hasValue = array.some(function(item) {
return item === value;
});
if (hasValue) {
// code
}
Example: The below example Array.prototype.some() to check if the json key value is null in JavaScript.
const jsonData = {
key1: null,
key2: "value2",
key3: undefined,
};
const res = Object.values(jsonData).
some(value => value === null);
if (res) {
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()
Contact Us