I need to make a simple generic filter function that makes a verification on javascript event 'change' a search.
I have the following Json:
{
"name": "Ramsey Cummings",
"gender": "male",
"age": 12.5,
"address": {
"state": {
"post": "20042",
"name": "South Carolina"
},
"city": "Glendale"
}
}
How do i make a generic approach to get all leafs searched in my object?
I added a user mapping that i will pass to my function, example:
this.filterDataMap = [
{
key: 'address', value: 'state.name'
}
]
My filter function looks as follows:
filterTable(array, value) {
let hash = [];
let objectProperties = Object.getOwnPropertyNames(array[0]);
$.grep(array, (item, i) => {
let isValidItem = false;
objectProperties.forEach(element => {
let mapElementArray = $.grep(this.filterDataMap, i => i.key === element);
if (mapElementArray.length > 0) {
debugger;
let mapperValue = ''.concat(element, '.', mapElementArray[0].value);
if (item[mapperValue] !== undefined &&
item[mapperValue].toString().toLowerCase().indexOf(value) !== -1 || !value)
isValidItem = true;
}
else if (item[element] !== undefined && item[element].toString().toLowerCase().indexOf(value) !== -1 || !value)
isValidItem = true;
});
if (isValidItem)
hash.push(item);
})
return hash;
}
I found in a different post here that is should be possible to concatenate my object properties like item['address.state.name'] but when i do it i get the undefined result.