How to use the xorWith function from lodash

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

lodash.xorWith returns an array of unique values, comparing them with a custom comparator function.

436
437
438
439
440
441
442
443
444
445
module.exports.without             = _.without;
module.exports.words               = _.words;
module.exports.wrap                = _.wrap;
module.exports.xor                 = _.xor;
module.exports.xorBy               = _.xorBy;
module.exports.xorWith             = _.xorWith;
module.exports.zip                 = _.zip;
module.exports.zipObject           = _.zipObject;
module.exports.zipObjectDeep       = _.zipObjectDeep;
module.exports.zipWith             = _.zipWith;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

183
184
185
186
187
188
189
190
191
192
193
194
195
console.log(xor); // => [1, 3]


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


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


const zip = _.zip(['a', 'b'], [1, 2], [true, false]);
console.log(zip); // => [['a', 1, true], ['b', 2, false]]
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.xorWith work?

lodash.xorWith is a function in the Lodash library that takes in two arrays and a comparator function, and returns an array of unique values that are present in only one of the input arrays, as determined by the comparator function.

517
518
519
520
521
522
523
524
525
526
527
528
//xorBy2---> [ { x: 2 } ]


//_.xorWith(array,[comparator])
let xorWith1 = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
let xorWith2 = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
let xorWith3 = _.xorWith(xorWith1, xorWith2, _.isEqual);
console.log('xorWith3--->', xorWith3);
//xorWith3---> [ { x: 2, y: 1 }, { x: 1, y: 1 } ]


//_.zip([arrays])
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const _ = require("lodash");

const array1 = [
  { x: 1, y: 2 },
  { x: 2, y: 1 },
];
const array2 = [
  { x: 1, y: 2 },
  { x: 2, y: 1 },
  { x: 3, y: 3 },
];

const result = _.xorWith(array1, array2, _.isEqual);
console.log(result); // [{ x: 3, y: 3 }]

In this example, lodash.xorWith is used to find the symmetric difference between two arrays (array1 and array2) of objects. The third argument, _.isEqual, is a custom comparator function that is used to compare the objects in the arrays. The function returns an array containing the objects that exist in only one of the arrays. In this case, the result is [{ x: 3, y: 3 }], which is the object that only exists in array2.

Other functions in lodash

Sorted by popularity

function icon

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