I'm basically trying to implement a search for any given value should look in the array of object key values(there can also be nested objects). Here is an example. The below function will take an object and a query to search in array objects key values. So, if a match is found it should filter from that array.
function searchObj (obj, query) {
for (var key in obj) {
var value = obj[key];
if (typeof value === 'object') {
searchObj(value, query);
}
if (typeof value === 'string' && value.toLowerCase().indexOf(query.toLowerCase()) > -1) {
return obj;
}
}
}
here is the dummy data
var demoData=[
{id:1,desc:{original:'trans1'},date:'2017-07-16'},
{id:2,desc:{original:'trans2'},date:'2017-07-12'},
{id:3,desc:{original:'trans3'},date:'2017-07-11'},
{id:4,desc:{original:'trans4'},date:'2017-07-15'}
];
here is the array I'm filtering object of the match
var searchFilter = demoData.filter(function(obj){
return searchObj(obj, 'trans1');
});
console.log(searchFilter);
for example: if I call searchObj(obj,'2017-07-15')
it returns that particular object but if I search for trans1
or simply trans
it should look into the object and then return the match. I'm kinda stuck now any help would be appreciated. Thanks.