javascript - underscore/lodash unique by multiple properties -


I have duplicate objects and I am trying to get a unique entry, where specificity is defined by subset For example, for the properties of the object,

  {a: "1", b: "1", c: "2"}   

And in the C specificity

I can do something like this

  _ Uniq (myArray, function (element) {return element.a + "_" + element + b});   

I was hoping that I could

  _ Uniq (myArray, function (element) {return {a: element.a, b: element B}});   

But it does not work, is there anything I can do, or do I need to make a comparable representation of the object while comparing multiple properties?

There is not a simple way to do this, unfortunately. For this, lacking in writing your own work, you will need to return something that can be compared directly with equality (as your first example).

One method would be just . )

  _. UniqBy (myArray, function (elem) {return [elem.a, elem.b] .join ();});   

Alternatively, you can use whatever you can not to remove or delete. From there, you can _ Value can be used with .join () , or even just JSON.stringify :

 < Code> _ UniqBy (myArray, function (elem) {return JSON.stringify (_. (Elem, ['a', 'b'])}}};   

Objects are not conclusive in the form, so you can stick to the clear array approach.

For PS uniqBy with uniq Lodash & Lt; 4

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -