This script allows you to search nested arrays and check if an element (child) exists.
If you use AJAX to send/receive data from a backend, you might find yourself constantly writing code that checks data existence. I wrote this to use within my own applications, it clears up my code, and prevents errors when I'm trying to JSON data from AJAX response.
Features
- Simple & Easy to add to your projects.
- Cleaner code, at least for me...
- Lower the risk of errors that break your code.
- Ability to use callback functions that are only called when child in found in nest.
There are two ways that you can use the function.
var data = {
things: {
books: {
count: 1
}
}
};
The callback function is only called when the child is found in parent.
childSearch(data, "things.books.count", function(element) {
//Do something with element
});
Returns a boolean depending on if the child was found in parent.
childSearch(data, "things.books.count");