Underscore.js, remove duplicates in array of objects based on key value
With Underscore, use _.uniq
with a custom transformation, a function like _.property('name')
would do nicely or just 'name'
, as @Gruff Bunny noted in the comments :
var mySubArray = _.uniq(myArray, 'name');
And a demo http://jsfiddle.net/nikoshr/02ugrbzr/If you use Lodash and not Underscore, go with the example given by @Jacob van Lingen in the comments and use _.uniqBy:
var mySubArray = _.uniqBy(myArray, 'name')
Remove duplicates from object array - Underscore
Because two objects are not ==
or ===
, just because they have the same keys, you will need to use the [iterator]
argument of the uniq()
function;
_.uniq(myArray, function (item) {
return item.x + item.y;
});
Note you'll need to return a unique string combination; consider the case of { x: 11, y: 1 }
and { x: 1, y: 11 }
; my code will resolve both to 111
, and treat them as the same. Something like:
_.uniq(myArray, function (item) {
return 'x:' + item.x + 'y:' + item.y;
});
... would be more resilient, but it depends on the values of your data, of course. How to remove duplicate item form array of string objects with _underscoreJS
You can use Array.map
to groups arrays, Array.flat
to marge arrays and Set
to remove duplicate.
let input1 = ["PC", "PS3,Xbox 360,PC,PS4,Xbox One,Nintendo Switch", "PC,PS4,Xbox One,Nintendo Switch", "PS4,PC", "PS4,PS5,Xbox One,Nintendo Switch,PC", "PC,PS4,Xbox One", "PC,Android"];
let input2 = { "data": [{ "userId": 8, "game": "League of legends", "playTime": 500, "genre": "MOBA", "platforms": ["PC"] }, { "userId": 7, "game": "World of warcraft", "playTime": 1500, "genre": "MMORPG", "platforms": ["PC"] }, { "userId": 88, "game": "Dark Souls", "playTime": 109, "genre": "Action RPG", "platforms": ["PS3", "Xbox 360", "PC", "PS4", "Xbox One", "Nintendo Switch"] }, { "userId": 88, "game": "The Witcher 3: Wild Hunt", "playTime": 9, "genre": "RPG", "platforms": ["PC", "PS4", "Xbox One", "Nintendo Switch"] }, { "userId": 1, "game": "The last of us 2", "playTime": 100, "genre": "FPS", "platforms": ["PS4", "PC"] }, { "userId": 7, "game": "Hitman 3", "playTime": 60, "genre": "Stealth", "platforms": ["PS4", "PS5", "Xbox One", "Nintendo Switch", "PC"] }, { "userId": 99, "game": "Minecraft", "playTime": 1002, "genre": "Sandbox", "platforms": ["PC"] }, { "userId": 7, "game": "Hearthstone", "playTime": 1000, "genre": "Card Game", "platforms": ["PC"] }, { "userId": 7, "game": "FIFA", "playTime": 2000, "genre": "Sport", "platforms": ["PC", "PS4", "Xbox One"] }, { "userId": 2, "game": "The Witcher 3: Wild Hunt", "playTime": 78, "genre": "RPG", "platforms": ["PC", "PS4", "Xbox One", "Nintendo Switch"] }, { "userId": 47, "game": "League of legends", "playTime": 850, "genre": "MOBA", "platforms": ["PC"] }, { "userId": 2, "game": "Among Us", "playTime": 5000, "genre": "Multiplayer", "platforms": ["PC", "Android"] }, { "userId": 2, "game": "Valorant", "playTime": 2000, "genre": "FPS", "platforms": ["PC"] }, { "userId": 9, "game": "Valorant", "playTime": 80, "genre": "FPS", "platforms": ["PC"] }, { "userId": 9, "game": "Dark Souls", "playTime": 109, "genre": "RPG", "platforms": ["PS3", "Xbox 360", "PC", "PS4", "Xbox One", "Nintendo Switch"] }, { "userId": 9, "game": "The Witcher 3: Wild Hunt", "playTime": 900, "genre": "RPG", "platforms": ["PC", "PS4", "Xbox One", "Nintendo Switch"] }, { "userId": 24, "game": "League of legends", "playTime": 300, "genre": "MOBA", "platforms": ["PC"] }, { "userId": 24, "game": "World of warcraft", "playTime": 800, "genre": "MMORPG", "platforms": ["PC"] }, { "userId": 54, "game": "Minecraft", "playTime": 231, "genre": "Sandbox", "platforms": ["PC"] }, { "userId": 7, "game": "Minecraft", "playTime": 777, "genre": "Sandbox", "platforms": ["PC"] }, { "userId": 7, "game": "Hitman 3", "playTime": 90, "genre": "Stealth", "platforms": ["PS4", "PS5", "Xbox One", "Nintendo Switch", "PC"] }] }
let removeDuplicate = (array, procedure) => [...new Set(array.map(procedure).flat())];
let result1 = removeDuplicate(input1, item => item.split(","));
let result2 = removeDuplicate(input2.data, ({ platforms }) => platforms);
console.log({ result1, result2 });
Underscore: Remove object having some duplicate properties
For example:
_.uniq(data, function(x) { return x.LATITUDE + "/" + x.LONGITUDE })
Basically, you provide a function that is supposed to return a hash value based on selected properties. Filter unique & duplicate objects in array using underscore
It might be easier to introduce a temporary variable to hold the groups:
var data = [{"origin":"SJU","dest":"JFK","rank":48},{"origin":"JFK","dest":"SJU","rank":21},{"origin":"IAD","dest":"LAX","rank":31},{"origin":"LAS","dest":"SJU","rank":21}]
var groups = _.groupBy(data, function(item) {
return [item.origin, item.dest].sort();
});
Then:var duplicates = [],
singles = [];
_.each(groups, function(group) {
if (group.length > 1) {
duplicates.push.apply(duplicates, group);
} else {
singles.push(group[0]);
}
});
Demo Remove duplicates from JavaScript Array of Objects based on Key
From the little that i understand.
you can use reduce for this
check the code snippet
var fileList = [{ identifier: "auto-1", file: "mp.mp3" }, { identifier: "auto-2", file: "ss.mp3" }, { identifier: "auto-3", file: "mj.mp3" }, { identifier: "type1", file: "ss.mp3" }, { identifier: "type2", file: "squirrel-chatter.mp3" }, { identifier: "type3", file: "mj.mp3" }]var fileList2 = [{ identifier: "auto-1", file: "mp.mp3"}, { identifier: "type1", file: "mp.mp3"}]
var result;
result = fileList2.reduce(function callback(result, key, index, keysArray) { debugger; let indx = result.findIndex((item) => item.file === key.file) if (indx > -1) { result[indx].identifier = (key.identifier.includes("type")) ? key.identifier : result[indx].identifier } else { result.push(key) } return result}, [])
console.log(result)
Remove duplicates in an object array Javascript
Vanilla JS version:
const list = [{x:1,y:2}, {x:3,y:4}, {x:5,y:6}, {x:1,y:2}];
function dedupe(arr) { return arr.reduce(function(p, c) {
// create an identifying id from the object values var id = [c.x, c.y].join('|');
// if the id is not found in the temp array // add the object to the output array // and add the key to the temp array if (p.temp.indexOf(id) === -1) { p.out.push(c); p.temp.push(id); } return p;
// return the deduped array }, { temp: [], out: [] }).out;}
console.log(dedupe(list));
Related Topics
Why Are Es6 Classes Not Hoisted
JavaScript Timestamp to Relative Time
How to Sort an Associative Array by Its Values in JavaScript
How to Parseint a String with Leading 0
How to Check "Checkbox" Dynamically - Jquery Mobile
JavaScript Call Nested Function
How to Bind Bootstrap Popover on Dynamic Elements
How Do We Set Remote in Typeahead.Js
How to Avoid Freezing the Browser When Doing Long-Running Computations in JavaScript
How to Replace Last Occurrence of Characters in a String Using JavaScript
Initializing Select with Angularjs and Ng-Repeat
How Does Setinterval and Settimeout Work
Jquery:Select All Element with Custom Attribute
How to Sign into Google with Selenium Automation Because of "This Browser or App May Not Be Secure."
How to Know When I'Ve Stopped Scrolling
Angularjs - Cancel Route Change Event
Why Do I Need to Await an Async Function When It Is Not Supposedly Returning a Promise