3

I am trying to get the unique category from the following array using loadsh,

[{
  "listingId": "p106a904a-b8c6-4d2d-a364-0d21e3505010",
  "section": "section1",
  "category": "VIP PASS ",
  "type": "paper",
  "availableTickets": 1
}, {
  "listingId": "p106904a-b8c6-4d2d-a364-0d21e3505010",
  "section": "section1",
  "category": "VIP PASS ",
  "type": "paper",
  "availableTickets": 2
}, {
  "listingId": "pc8f54389-4e58-482a-9535-6917c2948764",
  "section": "1",
  "category": "WIP",
  "type": "paper",
  "availableTickets": 1
}]

This is what i have tried

 this.categories = _.uniq(this.listings, function (test: ListDAO) { return test.category; });

but the above returns the same array again. how can i get the output result as,

VIP PASS and WIP

0

3 Answers 3

3

Without lodash and using .reduce:

let arr2 = arr.reduce((a, i) => a.indexOf(i.category) > -1 ? a : a.concat(i.category), []);

https://jsfiddle.net/42my6p08/

Sign up to request clarification or add additional context in comments.

7 Comments

how to change this to support typescript
@faceturn that is valid js/typescript (as is mine)
is there a way to automatically remove empty string?
@faceturn - You can use .filter afterwards - .filter(c => !!c)
this.listings.reduce((a, i) => a.indexOf(i.category) > -1 ? a : a.concat(i.category), []).filter(c => !!c);
|
2

You need to use uniqBy as uniq only accepts a regular array with no callback for each.

https://lodash.com/docs/4.17.4#uniqBy

You can try this:

this.categories = _.uniqBy(this.listings, ({ category }) => category);

If you want just the strings (as per comments) you can just do:

const getCategory = ({ category }) => category;

this.categories = _.uniqBy(this.listings, getCategory).map(getCategory);

(can also use the same callback function from your OP instead of mine.)

15 Comments

it does not work with typescript , there is an error on category
You can still use your same old callback function (test: ListDAO) { return test.category; } @faceturn
this.ticketCategories = _.uniqBy(this.listings, function (test: ListDAO) { return test.category; });
the above return correct unique objects , not string values as mentioned
again you can use your same callback... I was just making it smaller using es6 fanciness.
|
0

A solution using Map and reduce.

var arr = [{
  "listingId": "p106a904a-b8c6-4d2d-a364-0d21e3505010",
  "section": "section1",
  "category": "VIP PASS ",
  "type": "paper",
  "availableTickets": 1
}, {
  "listingId": "p106904a-b8c6-4d2d-a364-0d21e3505010",
  "section": "section1",
  "category": "VIP PASS ",
  "type": "paper",
  "availableTickets": 2
}, {
  "listingId": "pc8f54389-4e58-482a-9535-6917c2948764",
  "section": "1",
  "category": "WIP",
  "type": "paper",
  "availableTickets": 1
}];

var unique = arr.reduce((map, o) => (map.has(o.category) ? map : map.set(o.category)),new Map());
console.log(Array.from(unique.keys()));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.