Sunday, June 2, 2024
44
rated 0 times [  50] [ 6]  / answers: 1 / hits: 17515  / 8 Years ago, thu, may 19, 2016, 12:00:00

My collection looks like this.



  var list = [{id:'12345', sequence:null}, {id:'12346', sequence:null}, {id:'12347', sequence:null}, {id:'12348', sequence:1}, {id:'12348', sequence:2}, {id:'12349', sequence:1}, {id:'12349', sequence:1}];


I am trying to get a unique list so that object with same id AND sequence will only return one of the objects (we have 2 here- {id:'12349', sequence:1})



my code



  var uniqueList = _.uniq(list, function(obj) {
return obj.id && obj.sequence;
});


can we use lodash uniq like this? what is the approach to fix this issue?


More From » underscore.js

 Answers
11

You can use uniqBy() to compute the criterion by which the list is generated wherein the iteratee callback returns the joined value of id and sequence of each item of the colection. The solution below works great when you only want to have specific properties to be the criterion of uniqueness especially when each object in your collection may not only have the id and sequence properties.



var result = _.uniqBy(list, v => [v.id, v.sequence].join());




var list = [{
id: '12345',
sequence: null
}, {
id: '12346',
sequence: null
}, {
id: '12347',
sequence: null
}, {
id: '12348',
sequence: 1
}, {
id: '12348',
sequence: 2
}, {
id: '12349',
sequence: 1
}, {
id: '12349',
sequence: 1
}, {
id: '123490',
sequence: 1,
extra: 1
}, {
id: '123490',
sequence: 1,
extra: 2
}];

var result = _(list)
.uniqBy(v => [v.id, v.sequence].join())
.value();

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

<script src=https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.js></script>





Alternatively, if you have properties that contains non-primitive values (objects or arrays) to be used as the criterion then you can still use the solution above but instead joining the array of values, you have to use JSON.stringify()



var result = _.uniqBy(list, v => JSON.stringify([v.id, v.sequence]));




var list = [{
id: '12345',
sequence: null
}, {
id: '12346',
sequence: null
}, {
id: '12347',
sequence: null
}, {
id: '12348',
sequence: 1
}, {
id: '12348',
sequence: 2
}, {
id: '12349',
sequence: 1
}, {
id: '12349',
sequence: 1
}, {
id: '123490',
sequence: 1,
extra: 1
}, {
id: '123490',
sequence: 1,
extra: 2
}];

var result = _.uniqBy(list, v => JSON.stringify([v.id, v.sequence]));

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

<script src=https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.js></script>





UPDATE:



For the lodash3 solution, for properties with primitive values, you can still use the pattern in the first solution using uniq().



var result = _.uniq(list, v => [v.id, v.sequence].join());




var list = [{
id: '12345',
sequence: null
}, {
id: '12346',
sequence: null
}, {
id: '12347',
sequence: null
}, {
id: '12348',
sequence: 1
}, {
id: '12348',
sequence: 2
}, {
id: '12349',
sequence: 1
}, {
id: '12349',
sequence: 1
}, {
id: '123490',
sequence: 1,
extra: 1
}, {
id: '123490',
sequence: 1,
extra: 2
}];

var result = _.uniq(list, v => [v.id, v.sequence].join());

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

<script src=https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js></script>





And for the non-primitive properties, you can still use the 2nd example above with uniq()



var result = _.uniq(list, v => JSON.stringify([v.id, v.sequence]));




var list = [{
id: '12345',
sequence: null
}, {
id: '12346',
sequence: null
}, {
id: '12347',
sequence: null
}, {
id: '12348',
sequence: 1
}, {
id: '12348',
sequence: 2
}, {
id: '12349',
sequence: 1
}, {
id: '12349',
sequence: 1
}, {
id: '123490',
sequence: 1,
extra: 1
}, {
id: '123490',
sequence: 1,
extra: 2
}];

var result = _.uniq(list, v => JSON.stringify([v.id, v.sequence]));

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

<script src=https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js></script>




[#62110] Tuesday, May 17, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaya

Total Points: 531
Total Questions: 107
Total Answers: 100

Location: United States Minor Outlying Island
Member since Sat, May 28, 2022
2 Years ago
;