I am facing an issue with dynamically writing this code.
How can I create a function called testtest
in a dynamic way, considering that the data might have different keys and values in the future?
Here is how my JSON object looks:
data = [
{
name: 'John',
id: 1,
colour: 'Green',
pet: 'Dog'
},
{
name: 'Sarah',
id: 2,
colour: 'Purple',
pet: 'Cat'
},
{
name: 'Lindsay',
id: 3,
colour: 'Blue',
pet: 'Lizard'
},
{
name: 'Megan',
id: 4,
colour: 'Orange',
pet: 'Dog'
}
];
Additionally, I have a set of filter values defined as:
filterValues = {
name: '',
id: '',
colour: '',
pet: ''
};
Now, let's look at a function implementation:
createFilter(): (data: any, filter: string) => boolean {
let filterFunction = function(data, filter): boolean {
let searchTerms = JSON.parse(filter);
let result = this.testtest(data, searchTerms);
return result;
}
return filterFunction;
}
testtest(data, searchTerms): boolean {
return data.name.toLowerCase().indexOf(searchTerms.name) !== -1
&& data.id.toString().toLowerCase().indexOf(searchTerms.id) !== -1
&& data.colour.toLowerCase().indexOf(searchTerms.colour) !== -1
&& data.pet.toLowerCase().indexOf(searchTerms.pet) !== -1;
}