Question

I have the following array:

var tst = 
[
 {"topicId":1,"subTopicId":1,"topicName":"a","subTopicName":"11"},
 {"topicId":1,"subTopicId":2,"topicName":"a","subTopicName":"22"},
 {"topicId":1,"subTopicId":3,"topicName":"a","subTopicName":"33"},
 {"topicId":2,"subTopicId":x,"topicName":"b","subTopicName":"xx"},
 {"topicId":3,"subTopicId":y,"topicName":"c","subTopicName":"yy"}
]

What I would like to do is to use the _.uniq function of _lodash

_.uniq(array, [isSorted=false], [callback=identity], [thisArg])

to create this:

var t = 
[
 {"topicId":1,"subTopicId":1,"topicName":"a","subTopicName":"11"},
 {"topicId":2,"subTopicId":x,"topicName":"b","subTopicName":"xx"},
 {"topicId":3,"subTopicId":y,"topicName":"c","subTopicName":"yy"}
]

I understand I can do this using something like the following but I am not sure I understand how this matches to the function definition I see in the documentation:

var t = _.uniq(tst, 'topicId');

But as my array is always sorted then how can I use the option [isSorted=true]. Also is 'topicId' considered as the argument to sort by?

Was it helpful?

Solution

You have the answer in your question! From: http://lodash.com/docs#uniq

_.uniq(array, [isSorted=false], [callback=identity], [thisArg])

So in your example, you would use it as follows:

var t = _.uniq(tst, true, 'topicId');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top