Angular 6 multi field filter
up vote
0
down vote
favorite
I'm currently working on angular 6 app. I have a huge list of articles and want to do some filtering upon it. Right now it works real-time, there is no button to submit your filter options, it all happens as you type. I figure a way, but it has still some issues I can fix, but doesn't like the way I did it. I'm sure, there has to be something more elegant.
For better imagination those articles have category, title, author, tags. I am able to filter them according to category lets say... but I want to do some kind of filtering.
Example: Filter all articles from category 'sports', then filter all articles having substring 'goal' in title from that already filtered array, then filter those whos author is 'john' and then filter all with tag 'hockey'.
I ended up with huge amount of IF statements, which is not the right approach I would say. Can you please recommend me some better way to do this?
javascript angular frontend filtering
add a comment |
up vote
0
down vote
favorite
I'm currently working on angular 6 app. I have a huge list of articles and want to do some filtering upon it. Right now it works real-time, there is no button to submit your filter options, it all happens as you type. I figure a way, but it has still some issues I can fix, but doesn't like the way I did it. I'm sure, there has to be something more elegant.
For better imagination those articles have category, title, author, tags. I am able to filter them according to category lets say... but I want to do some kind of filtering.
Example: Filter all articles from category 'sports', then filter all articles having substring 'goal' in title from that already filtered array, then filter those whos author is 'john' and then filter all with tag 'hockey'.
I ended up with huge amount of IF statements, which is not the right approach I would say. Can you please recommend me some better way to do this?
javascript angular frontend filtering
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm currently working on angular 6 app. I have a huge list of articles and want to do some filtering upon it. Right now it works real-time, there is no button to submit your filter options, it all happens as you type. I figure a way, but it has still some issues I can fix, but doesn't like the way I did it. I'm sure, there has to be something more elegant.
For better imagination those articles have category, title, author, tags. I am able to filter them according to category lets say... but I want to do some kind of filtering.
Example: Filter all articles from category 'sports', then filter all articles having substring 'goal' in title from that already filtered array, then filter those whos author is 'john' and then filter all with tag 'hockey'.
I ended up with huge amount of IF statements, which is not the right approach I would say. Can you please recommend me some better way to do this?
javascript angular frontend filtering
I'm currently working on angular 6 app. I have a huge list of articles and want to do some filtering upon it. Right now it works real-time, there is no button to submit your filter options, it all happens as you type. I figure a way, but it has still some issues I can fix, but doesn't like the way I did it. I'm sure, there has to be something more elegant.
For better imagination those articles have category, title, author, tags. I am able to filter them according to category lets say... but I want to do some kind of filtering.
Example: Filter all articles from category 'sports', then filter all articles having substring 'goal' in title from that already filtered array, then filter those whos author is 'john' and then filter all with tag 'hockey'.
I ended up with huge amount of IF statements, which is not the right approach I would say. Can you please recommend me some better way to do this?
javascript angular frontend filtering
javascript angular frontend filtering
asked Nov 11 at 15:03
Patrick Star
193
193
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
This is what you could do to create a multifilter without many if statements.
Step one:
Create an object that performs various comparison functions:
let compareFunctions =
equal: function(a,b)
return a === b;
,
in: function(a,b)
return a.indexOf(b) !== -1
Step two:
Create a function that has the following parameters:
- key - The key of the field of your record you want to filter.
- value - The value you want to filter by
- compareFn - The comparison function to be used for this field
This function returns a function that executes the condition.
function condition(key, value, comparFn = compareFunctions.equal)
return function(data)
return comparFn(data[key],value);
Step three:
Create an array with "condition" functions representing your filter values:
let filterArray = [
condition('category', 'sports'),
condition('title', 'goal', compareFunctions.in),
condition('author', 'john'),
condition('tags', 'hokey', compareFunctions.in),
]
Step four:
Filter your record by calling your array of conditions functions for each entry and evaluating the result of each condition:
let result = dataset.filter(y =>
let resolved = filterArray.map(x => x(y))
return resolved.every(x => x === true);
)
Full example code:
let compareFunctions =
equal: function(a,b)
return a === b;
,
in: function(a,b)
return a.indexOf(b) !== -1
function condition(key, value, comparFn = compareFunctions.equal)
return function(data)
return comparFn(data[key],value);
let dataset = [
category: "sports",
title: "goal goal goal",
author: "john",
tags: ["hokey", "ice-hokey"]
,
category: "news",
title: "bla bla",
author: "Timo",
tags: ["news"]
,
category: "news",
title: "blub blub",
author: "alex",
tags: ["hokey", "ice-hokey"]
,
category: "sports",
title: "Kölner Haie bla bla",
author: "Timo",
tags: ["hokey", "ice-hokey"]
]
let filterArray = [
condition('category', 'sports'),
condition('title', 'goal', compareFunctions.in),
condition('author', 'john'),
condition('tags', 'hokey', compareFunctions.in),
]
//console.log(filterArray)
let result = dataset.filter(y =>
let resolved = filterArray.map(x => x(y))
return resolved.every(x => x === true);
)
console.log(result)
thank you. This is something I have been looking for.
– Patrick Star
Nov 12 at 7:37
Im glad to help.
– mr.void
Nov 12 at 13:05
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
This is what you could do to create a multifilter without many if statements.
Step one:
Create an object that performs various comparison functions:
let compareFunctions =
equal: function(a,b)
return a === b;
,
in: function(a,b)
return a.indexOf(b) !== -1
Step two:
Create a function that has the following parameters:
- key - The key of the field of your record you want to filter.
- value - The value you want to filter by
- compareFn - The comparison function to be used for this field
This function returns a function that executes the condition.
function condition(key, value, comparFn = compareFunctions.equal)
return function(data)
return comparFn(data[key],value);
Step three:
Create an array with "condition" functions representing your filter values:
let filterArray = [
condition('category', 'sports'),
condition('title', 'goal', compareFunctions.in),
condition('author', 'john'),
condition('tags', 'hokey', compareFunctions.in),
]
Step four:
Filter your record by calling your array of conditions functions for each entry and evaluating the result of each condition:
let result = dataset.filter(y =>
let resolved = filterArray.map(x => x(y))
return resolved.every(x => x === true);
)
Full example code:
let compareFunctions =
equal: function(a,b)
return a === b;
,
in: function(a,b)
return a.indexOf(b) !== -1
function condition(key, value, comparFn = compareFunctions.equal)
return function(data)
return comparFn(data[key],value);
let dataset = [
category: "sports",
title: "goal goal goal",
author: "john",
tags: ["hokey", "ice-hokey"]
,
category: "news",
title: "bla bla",
author: "Timo",
tags: ["news"]
,
category: "news",
title: "blub blub",
author: "alex",
tags: ["hokey", "ice-hokey"]
,
category: "sports",
title: "Kölner Haie bla bla",
author: "Timo",
tags: ["hokey", "ice-hokey"]
]
let filterArray = [
condition('category', 'sports'),
condition('title', 'goal', compareFunctions.in),
condition('author', 'john'),
condition('tags', 'hokey', compareFunctions.in),
]
//console.log(filterArray)
let result = dataset.filter(y =>
let resolved = filterArray.map(x => x(y))
return resolved.every(x => x === true);
)
console.log(result)
thank you. This is something I have been looking for.
– Patrick Star
Nov 12 at 7:37
Im glad to help.
– mr.void
Nov 12 at 13:05
add a comment |
up vote
0
down vote
This is what you could do to create a multifilter without many if statements.
Step one:
Create an object that performs various comparison functions:
let compareFunctions =
equal: function(a,b)
return a === b;
,
in: function(a,b)
return a.indexOf(b) !== -1
Step two:
Create a function that has the following parameters:
- key - The key of the field of your record you want to filter.
- value - The value you want to filter by
- compareFn - The comparison function to be used for this field
This function returns a function that executes the condition.
function condition(key, value, comparFn = compareFunctions.equal)
return function(data)
return comparFn(data[key],value);
Step three:
Create an array with "condition" functions representing your filter values:
let filterArray = [
condition('category', 'sports'),
condition('title', 'goal', compareFunctions.in),
condition('author', 'john'),
condition('tags', 'hokey', compareFunctions.in),
]
Step four:
Filter your record by calling your array of conditions functions for each entry and evaluating the result of each condition:
let result = dataset.filter(y =>
let resolved = filterArray.map(x => x(y))
return resolved.every(x => x === true);
)
Full example code:
let compareFunctions =
equal: function(a,b)
return a === b;
,
in: function(a,b)
return a.indexOf(b) !== -1
function condition(key, value, comparFn = compareFunctions.equal)
return function(data)
return comparFn(data[key],value);
let dataset = [
category: "sports",
title: "goal goal goal",
author: "john",
tags: ["hokey", "ice-hokey"]
,
category: "news",
title: "bla bla",
author: "Timo",
tags: ["news"]
,
category: "news",
title: "blub blub",
author: "alex",
tags: ["hokey", "ice-hokey"]
,
category: "sports",
title: "Kölner Haie bla bla",
author: "Timo",
tags: ["hokey", "ice-hokey"]
]
let filterArray = [
condition('category', 'sports'),
condition('title', 'goal', compareFunctions.in),
condition('author', 'john'),
condition('tags', 'hokey', compareFunctions.in),
]
//console.log(filterArray)
let result = dataset.filter(y =>
let resolved = filterArray.map(x => x(y))
return resolved.every(x => x === true);
)
console.log(result)
thank you. This is something I have been looking for.
– Patrick Star
Nov 12 at 7:37
Im glad to help.
– mr.void
Nov 12 at 13:05
add a comment |
up vote
0
down vote
up vote
0
down vote
This is what you could do to create a multifilter without many if statements.
Step one:
Create an object that performs various comparison functions:
let compareFunctions =
equal: function(a,b)
return a === b;
,
in: function(a,b)
return a.indexOf(b) !== -1
Step two:
Create a function that has the following parameters:
- key - The key of the field of your record you want to filter.
- value - The value you want to filter by
- compareFn - The comparison function to be used for this field
This function returns a function that executes the condition.
function condition(key, value, comparFn = compareFunctions.equal)
return function(data)
return comparFn(data[key],value);
Step three:
Create an array with "condition" functions representing your filter values:
let filterArray = [
condition('category', 'sports'),
condition('title', 'goal', compareFunctions.in),
condition('author', 'john'),
condition('tags', 'hokey', compareFunctions.in),
]
Step four:
Filter your record by calling your array of conditions functions for each entry and evaluating the result of each condition:
let result = dataset.filter(y =>
let resolved = filterArray.map(x => x(y))
return resolved.every(x => x === true);
)
Full example code:
let compareFunctions =
equal: function(a,b)
return a === b;
,
in: function(a,b)
return a.indexOf(b) !== -1
function condition(key, value, comparFn = compareFunctions.equal)
return function(data)
return comparFn(data[key],value);
let dataset = [
category: "sports",
title: "goal goal goal",
author: "john",
tags: ["hokey", "ice-hokey"]
,
category: "news",
title: "bla bla",
author: "Timo",
tags: ["news"]
,
category: "news",
title: "blub blub",
author: "alex",
tags: ["hokey", "ice-hokey"]
,
category: "sports",
title: "Kölner Haie bla bla",
author: "Timo",
tags: ["hokey", "ice-hokey"]
]
let filterArray = [
condition('category', 'sports'),
condition('title', 'goal', compareFunctions.in),
condition('author', 'john'),
condition('tags', 'hokey', compareFunctions.in),
]
//console.log(filterArray)
let result = dataset.filter(y =>
let resolved = filterArray.map(x => x(y))
return resolved.every(x => x === true);
)
console.log(result)
This is what you could do to create a multifilter without many if statements.
Step one:
Create an object that performs various comparison functions:
let compareFunctions =
equal: function(a,b)
return a === b;
,
in: function(a,b)
return a.indexOf(b) !== -1
Step two:
Create a function that has the following parameters:
- key - The key of the field of your record you want to filter.
- value - The value you want to filter by
- compareFn - The comparison function to be used for this field
This function returns a function that executes the condition.
function condition(key, value, comparFn = compareFunctions.equal)
return function(data)
return comparFn(data[key],value);
Step three:
Create an array with "condition" functions representing your filter values:
let filterArray = [
condition('category', 'sports'),
condition('title', 'goal', compareFunctions.in),
condition('author', 'john'),
condition('tags', 'hokey', compareFunctions.in),
]
Step four:
Filter your record by calling your array of conditions functions for each entry and evaluating the result of each condition:
let result = dataset.filter(y =>
let resolved = filterArray.map(x => x(y))
return resolved.every(x => x === true);
)
Full example code:
let compareFunctions =
equal: function(a,b)
return a === b;
,
in: function(a,b)
return a.indexOf(b) !== -1
function condition(key, value, comparFn = compareFunctions.equal)
return function(data)
return comparFn(data[key],value);
let dataset = [
category: "sports",
title: "goal goal goal",
author: "john",
tags: ["hokey", "ice-hokey"]
,
category: "news",
title: "bla bla",
author: "Timo",
tags: ["news"]
,
category: "news",
title: "blub blub",
author: "alex",
tags: ["hokey", "ice-hokey"]
,
category: "sports",
title: "Kölner Haie bla bla",
author: "Timo",
tags: ["hokey", "ice-hokey"]
]
let filterArray = [
condition('category', 'sports'),
condition('title', 'goal', compareFunctions.in),
condition('author', 'john'),
condition('tags', 'hokey', compareFunctions.in),
]
//console.log(filterArray)
let result = dataset.filter(y =>
let resolved = filterArray.map(x => x(y))
return resolved.every(x => x === true);
)
console.log(result)
edited Nov 11 at 16:07
answered Nov 11 at 16:01
mr.void
1,5252623
1,5252623
thank you. This is something I have been looking for.
– Patrick Star
Nov 12 at 7:37
Im glad to help.
– mr.void
Nov 12 at 13:05
add a comment |
thank you. This is something I have been looking for.
– Patrick Star
Nov 12 at 7:37
Im glad to help.
– mr.void
Nov 12 at 13:05
thank you. This is something I have been looking for.
– Patrick Star
Nov 12 at 7:37
thank you. This is something I have been looking for.
– Patrick Star
Nov 12 at 7:37
Im glad to help.
– mr.void
Nov 12 at 13:05
Im glad to help.
– mr.void
Nov 12 at 13:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250009%2fangular-6-multi-field-filter%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown