简体   繁体   中英

get uniq [key,value] from object array with lodash

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?

I could do it in two separate lines:

var uniq = _.uniqBy(vm.productList, 'categoryid');
vm.categories = _.map(uniq, _.partialRight(_.pick, ['categoryid', 'category']))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM