1

I have an object array, products, with properties "categoryid, categoryname, name, price". I want to return an object array with just "categoryid, categoryname" from this but since categoryid exist multiple times, I want only the unique results.

I can get both fields with this code but it won't be unique.

_.map(products, _.partialRight(_.pick, ['categoryid', 'category']))

How can I make this unique?

3
  • Would you be okay with JS only solution or Lodash is a must? Commented May 6, 2016 at 6:17
  • 3
    stackoverflow.com/questions/31740155/… Commented May 6, 2016 at 6:20
  • 1
    As @gurvinder372 mentioned you can get the unique values first and then map them out. _(products).uniqBy('categoryId').map(_.partialRight(_.pick, ['categoryid', 'categroyname'])).value() Commented May 6, 2016 at 6:32

1 Answer 1

2

I could do it in two separate lines:

var uniq = _.uniqBy(vm.productList, 'categoryid');
vm.categories = _.map(uniq, _.partialRight(_.pick, ['categoryid', 'category']))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.