How to use the unionWith function from lodash

Find comprehensive JavaScript lodash.unionWith code examples handpicked from public code repositorys.

lodash.unionWith is a function in the Lodash library that creates a new array with unique values from two or more input arrays, using a custom comparator function to determine uniqueness.

410
411
412
413
414
415
416
417
418
419
module.exports.truthyAll           = _.truthyAll;
module.exports.unary               = _.unary;
module.exports.unescape            = _.unescape;
module.exports.union               = _.union;
module.exports.unionBy             = _.unionBy;
module.exports.unionWith           = _.unionWith;
module.exports.uniq                = _.uniq;
module.exports.uniqBy              = _.uniqBy;
module.exports.uniqWith            = _.uniqWith;
module.exports.uniqueId            = _.uniqueId;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

156
157
158
159
160
161
162
163
164
165
166
167
168
console.log(union); // => [2, 1]


const unionBy = _.unionBy([2.1], [1.2, 2.3], Math.floor);
console.log(unionBy); // => [2.1, 1.2]


const unionWith = _.unionWith([2.1], [1.2, 2.3], (a, b) => Math.floor(a) === Math.floor(b));
console.log(unionWith); // => [2.1, 1.2]


const uniq = _.uniq([2, 1, 2]);
console.log(uniq); // => [2, 1]
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.unionWith work?

lodash.unionWith is a function in the Lodash library that creates a new array with unique values from two or more input arrays, using a custom comparator function to determine uniqueness.

It takes two arguments: the first argument is an array of arrays to process, and the second argument is a custom comparator function that is used to compare the values in the arrays.

The comparator function is called with two arguments: the first argument is the value from the first array being compared, and the second argument is the value from the second array being compared. If the comparator function returns a truthy value for two values, they are considered to be duplicates and only one value will be included in the resulting array.

The resulting array is a new array that contains unique values from all the input arrays, with duplicates removed based on the custom comparator function. If there are no duplicates between the input arrays, the resulting array will simply be a concatenation of the input arrays.

lodash.unionWith can be useful in situations where you need to find the unique values from multiple arrays using a custom comparison function, such as when comparing objects by a specific property or comparing values with custom logic.

111
112
113
114
115
116
117
118
119
120

tids_unread.forEach((t, i) => {
    isTopicsFollowed[t.value] = unreadFollowed[i];
});

const unreadTopics = _.unionWith(categoryTids, followedTids, (a, b) => a.value === b.value)
    .filter(t => !ignoredTids.includes(t.value) &&
            (!userReadTimes[t.value] || t.score > userReadTimes[t.value]))
    .concat(tids_unread.filter(t => !ignoredTids.includes(t.value)))
    .sort((a, b) => b.score - a.score);
fork icon0
star icon0
watch icon1

+ 7 other calls in file

457
458
459
460
461
462
463
464
465
466
467
468
//unionBy2---> [ { x: 1 }, { x: 2 } ]


//_.unionWith([arrays],[comparator])
let unionWith1 = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
let unionWith2 = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
let unionWith3 = _.unionWith(unionWith1, unionWith2, _.isEqual);
console.log('unionWith3--->', unionWith3);
//unionWith3---> [ { x: 1, y: 2 }, { x: 2, y: 1 }, { x: 1, y: 1 } ]


//_.uniq(array)
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const _ = require("lodash");

// Create two arrays of objects to compare
const array1 = [
  { id: 1, name: "Alice" },
  { id: 2, name: "Bob" },
  { id: 3, name: "Charlie" },
];

const array2 = [
  { id: 2, name: "Bob" },
  { id: 3, name: "Charlie" },
  { id: 4, name: "Dave" },
];

// Define a custom comparator function to compare the objects by id
const comparatorFn = (obj1, obj2) => obj1.id === obj2.id;

// Use lodash unionWith to create a new array with unique values from the two arrays based on the comparator function
const result = _.unionWith(array1, array2, comparatorFn);

console.log(result);
// Output: [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' }, { id: 4, name: 'Dave' } ]

In this example, lodash.unionWith is used to create a new array with unique values from array1 and array2, based on the comparatorFn function. Since comparatorFn compares the objects by id, the resulting array contains only one object with id of 2 and 3, and includes all other objects.

602
603
604
605
606
607
608
609
610
611

let mergedArr = [];
allTumorFactRelnArr.forEach(function (reln) {
    // https://lodash.com/docs/4.17.4#union
    // Creates an array of unique values, in order, from all given arrays
    mergedArr = _.unionWith(mergedArr, reln, _.isEqual);
});

// Sort the fact relationships by the item's index in the order array
let sortedAllFactRelationships = this.sortByProvidedOrder(mergedArr, order);
fork icon0
star icon0
watch icon0

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)