Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Check whether a value exists in JSON object?
In JavaScript, you can check whether a value exists in a JSON object (or array of objects) using several approaches. Let's explore different methods to find values efficiently.
Consider the following JSON object structure:
var apiJSONObject = [
{subjectName: "MySQL"},
{subjectName: "Java"},
{subjectName: "JavaScript"},
{subjectName: "MongoDB"}
]
Using for Loop (Traditional Approach)
The basic approach uses a for loop to iterate through the array and check each object:
var apiJSONObject = [
{subjectName: "MySQL"},
{subjectName: "Java"},
{subjectName: "JavaScript"},
{subjectName: "MongoDB"}
];
var found = false;
for(var i = 0; i < apiJSONObject.length; i++){
if(apiJSONObject[i].subjectName == "JavaScript"){
console.log("The search found in JSON Object");
found = true;
break;
}
}
if(!found){
console.log("Value not found");
}
The search found in JSON Object
Using Array.some() Method (Recommended)
The some() method provides a cleaner and more functional approach:
var apiJSONObject = [
{subjectName: "MySQL"},
{subjectName: "Java"},
{subjectName: "JavaScript"},
{subjectName: "MongoDB"}
];
var exists = apiJSONObject.some(function(obj) {
return obj.subjectName === "JavaScript";
});
console.log("Value exists:", exists);
// Check for non-existent value
var notExists = apiJSONObject.some(function(obj) {
return obj.subjectName === "Python";
});
console.log("Python exists:", notExists);
Value exists: true Python exists: false
Using Array.find() Method
Use find() when you need to retrieve the actual object that contains the value:
var apiJSONObject = [
{subjectName: "MySQL", difficulty: "Medium"},
{subjectName: "Java", difficulty: "Hard"},
{subjectName: "JavaScript", difficulty: "Easy"},
{subjectName: "MongoDB", difficulty: "Medium"}
];
var foundObject = apiJSONObject.find(function(obj) {
return obj.subjectName === "JavaScript";
});
if(foundObject) {
console.log("Found:", foundObject);
console.log("Difficulty:", foundObject.difficulty);
} else {
console.log("Object not found");
}
Found: { subjectName: 'JavaScript', difficulty: 'Easy' }
Difficulty: Easy
Comparison of Methods
| Method | Returns | Use Case | Performance |
|---|---|---|---|
| for loop | Custom logic | Complex conditions | Fast, stops early with break |
| Array.some() | Boolean | Check existence only | Good, stops at first match |
| Array.find() | Object or undefined | Need the actual object | Good, stops at first match |
Checking Multiple Properties
You can also check for values across multiple properties:
var apiJSONObject = [
{subjectName: "MySQL", category: "Database"},
{subjectName: "Java", category: "Programming"},
{subjectName: "JavaScript", category: "Programming"},
{subjectName: "MongoDB", category: "Database"}
];
var hasProgLanguage = apiJSONObject.some(function(obj) {
return obj.category === "Programming" && obj.subjectName === "JavaScript";
});
console.log("JavaScript programming language exists:", hasProgLanguage);
JavaScript programming language exists: true
Conclusion
Use Array.some() for simple existence checks and Array.find() when you need the actual object. Both methods are more readable and functional than traditional for loops.
Advertisements
