JSFiddle - React, Tailwind, and code Playground
HTML
<div id="result"></div>
JavaScript
function deepSearch (object, key, predicate) {
if (object.hasOwnProperty(key) && predicate(key, object[key]) === true) return object
for (let i = 0; i < Object.keys(object).length; i++) {
let value = object[Object.keys(object)[i]];
if (typeof value === "object" && value != null) {
let o = deepSearch(object[Object.keys(object)[i]], key, predicate)
if (o != null) return o
}
}
return null
}
$(function() {
var myObject = [{
'title': 'some title',
'channel_id':'123we',
'options': [{
'channel_id':'abc',
'image':'http://asdasd.com/all-inclusive-block-img.jpg',
'title':'All-Inclusive',
'options': [{
'channel_id':'dsa2',
'title':'Some Recommends',
'options': {
'image':'http://www.asdasd.com',
'title':'Sandals',
'id':'2'
}
},
{
'channel_id':'dsa3',
'title':'Some Recommends',
'options': {
'image':'http://www.badasd.com',
'title':'Boots',
'id':'1'
}
}]
}]
}];
var result = deepSearch(myObject, 'title', (k, v) => v === 'Some Recommends');
if(result != null) {
$('#result').html('Found object: <br />');
for(var prop in result) {
$('#result').html($('#result').html() + prop + ': ' + result[prop] + '<br />');
}
}
else {
$('#result').html('Not Found.');
}
});