javascript - Get the set of all possible values for a categoric field -
this question has answer here:
from following object
items = [{ title = 'title 1', category = 'foo' }, { title = 'title 5', category = 'bar' }, { title = 'title n', category = 'bar' }, ]
which
- i receive dynamically @ runtime
- can have length
- where each category field can have 1 of
items.length
values
i want set of different values field category
.
in example above, result of
get_all_possible_categories(items)
would be
['foo','bar'].
how can implement
get_all_possible_categories(items) ?
just map values array, using set unique values
items = [{ title : 'title 1', category : 'foo' }, { title : 'title 5', category : 'bar' }, { title : 'title n', category : 'bar' } ]; function get_all_possible_categories(arr) { return [...new set(arr.map( x => x.category))]; } var result = get_all_possible_categories(items); console.log(result);
Comments
Post a Comment