Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
197
rated 0 times [  198] [ 1]  / answers: 1 / hits: 16990  / 8 Years ago, fri, september 16, 2016, 12:00:00

I have an array with objects:





var test = [
{
type: home,
name: My home,
country: DE,
postalZone: 1234,
cityName: GermanCity
},
{
type: home,
name: Some one else home,
country: DE,
postalZone: 1234,
cityName: GermanCity
},
{
type: home,
name: My home,
country: DE,
postalZone: 1234,
cityName: GermanCity
},
{
type: home1,
name: My home,
country: DE,
postalZone: 1234,
cityName: GermanCity
},
{
type: home,
name: My home,
country: DE,
postalZone: 1234,
cityName: GermanCity
}
];





I would like to mark the objects that they are duplicates, like this (notice the extra property, isDuplicate):





var test = [
{
type: home,
name: My home,
country: DE,
postalZone: 1234,
cityName: GermanCity
},
{
type: home,
name: Some one else home,
country: DE,
postalZone: 1234,
cityName: GermanCity
},
{
type: home,
name: My home,
country: DE,
postalZone: 1234,
cityName: GermanCity,
isDuplicate: true
},
{
type: home1,
name: My home,
country: DE,
postalZone: 1234,
cityName: GermanCity
},
{
type: home,
name: My home,
country: DE,
postalZone: 1234,
cityName: GermanCity,
isDuplicate: true
}
];





If one of the above values change (type, name, country, postalZone or cityName), the object is unique.



I have tried the following for testing purpose. Getting only the unique objects for type, does not work for me e.g.



_.uniq(test, type);


Still getting all the array objects. Ofcourse I want to check on more object keys, not only on type, but on all object keys (type, name, country, postalZone or cityName).



The native array function some, stops if it finds the first duplicate, for me it should not stop...



How can I achieve this?



I have Lodash v4.13.1.


More From » arrays

 Answers
6

Here is one solution with map() to change object if its found as duplicate and for finding duplicates i used find() and isEqual(). So if duplicate is found it will add property if not it will just return object.





var test = [{
type: home,
name: My home,
country: DE,
postalZone: 1234,
cityName: GermanCity
}, {
type: home,
name: Some one else home,
country: DE,
postalZone: 1234,
cityName: GermanCity
}, {
type: home,
name: My home,
country: DE,
postalZone: 1234,
cityName: GermanCity
}, {
type: home1,
name: My home,
country: DE,
postalZone: 1234,
cityName: GermanCity
}, {
type: home,
name: My home,
country: DE,
postalZone: 1234,
cityName: GermanCity
}];


var result = _.map(test, function(o, i) {
var eq = _.find(test, function(e, ind) {
if (i > ind) {
return _.isEqual(e, o);
}
})
if (eq) {
o.isDuplicate = true;
return o;
} else {
return o;
}
})

console.log(result)

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




[#60700] Tuesday, September 13, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ellisw

Total Points: 625
Total Questions: 92
Total Answers: 88

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
ellisw questions
Mon, Aug 23, 21, 00:00, 3 Years ago
Fri, Nov 20, 20, 00:00, 4 Years ago
Sat, Jun 20, 20, 00:00, 4 Years ago
Tue, Apr 21, 20, 00:00, 4 Years ago
;