Lodash remove duplicates from array
_.unique
no longer works for the current version of Lodash as version 4.0.0 has this breaking change. The functionality of _.unique
was splitted into _.uniq
, _.sortedUniq
, _.sortedUniqBy
, and _.uniqBy
.
You could use _.uniqBy
like this:
_.uniqBy(data, function (e) {
return e.id;
});
...or like this:
_.uniqBy(data, 'id');
Documentation: https://lodash.com/docs#uniqBy
For older versions of Lodash (< 4.0.0 ):
Assuming that the data should be uniqued by each object's id
property and your data is stored in data
variable, you can use the _.unique()
function like this:
_.unique(data, function (e) {
return e.id;
});
Or simply like this:
_.uniq(data, 'id');
Remove same property value duplicate from array of objects using Lodash
Use _.uniqBy()
the name
property instead:
const values = [{"name":"A","age":12},{"name":"B","age":1},{"name":"A","age":31}]
const result = _.uniqBy(values, 'name')
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Lodash remove duplicates from nested array
You can combine uniq and mapValues to achieve it.
let data = {
"deviceA": {
"serviceA": [
"Foo",
"Bar",
"Foo Bar A",
"Foo Bar A",
"Foo Bar A"
],
"serviceB": [
"Foo",
"Bar",
"Foo Bar B",
"Foo Bar B",
"Foo Bar B"
]
},
"deviceB": {
"serviceA": [
"A",
"A",
"Foo Bar A",
],
"serviceB": [
"B",
"B",
"Foo Bar B",
]
}
}
const items = _.mapValues(data, (device) => _.mapValues(device, (service) =>_.uniq(service)))
console.log(items)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
Remove duplicate items in objects array with lodash.js
Make sure that you use proper namings, that code works for me:
var arr = [
{
label: 'tagA',
value: 1
},
{
label: 'tagB',
value: 2
},
{
label: 'tagC',
value: 3
},
{
label: 'tagB',
value: 4
},
{
label: 'tagB',
value: 5
},
];
var uniq = _.uniqBy(arr, function(o){
return o.label;
});
console.log(uniq); // >> Returned an array with first 3 objects from array arr
Remove duplicate String(Ignore Case) from list in angular 8 using lodash
You can use _.uniqWith()
with a callback as following:
let duplicates = ['Hello', 'Hi', 'hello'];
let uniques = _.uniqWith(duplicates, (a,b) => a.toLowerCase() === b.toLowerCase())
console.log(uniques);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
Angular 9 - Remove duplicates from array of objects
Should works fine
let finalMessages = [];
let prevTimestamp = null;
messages.forEach((message, index) => {
if (moment(message.timestamp).isSame(moment(prevTimestamp), 'days')) {
message.timestamp = null
} else {
prevTimestamp = message.timestamp
}
finalMessages.push(message)
})
Related Topics
Difference Between "Process.Stdout.Write" and "Console.Log" in Node.Js
React - Getting a Component from a Dom Element for Debugging
How to Check If Element Has Any Children in JavaScript
How to Remove Leading and Trailing White Spaces from a Given HTML String
How to Round Numbers into a Reader Friendly Format? (E.G. $1.1K)
How to Set a Cookie with Expire Time
JavaScript Return Number of Days,Hours,Minutes,Seconds Between Two Dates
Creating an Array of Cumulative Sum in JavaScript
How to Send a Message to a Particular Client with Socket.Io
Keycode Values for Numeric Keypad
Why Not Always Use the Index as the Key in a Vue.Js for Loop
Typescript Compile to Single File
Can a Website Know If I am Running a Userscript
Pass JavaScript Variable to Flask Url_For
Remembering State Before App Goes in Foreground
How to Store a Global Value (Not Necessarily a Global Variable) in Jquery