How to use the differenceWith function from lodash

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

The lodash.differenceWith function returns an array of unique values from the first array that do not have an equivalent element in the second array, based on a comparator function.

3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
if (!_.isEqual(status.comms.interfaces.networkDevices, oldiplist)) {
  var newTelnetPorts = _.differenceWith(status.comms.interfaces.networkDevices, oldiplist, _.isEqual)
  if (newTelnetPorts.length > 0) {
    debug_log("Detected new device: " + newTelnetPorts[0].ip);
  }
  var removedTelnetPorts = _.differenceWith(oldiplist, status.comms.interfaces.networkDevices, _.isEqual)
  if (removedTelnetPorts.length > 0) {
    debug_log("No longer detecting device: " + removedTelnetPorts[0].ip);
  }
}
fork icon80
star icon133
watch icon24

+ 19 other calls in file

1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
if (!_.find(registrants, (r) => _.toString(r.memberId) === _.toString(winner.userId))) {
  throw new errors.BadRequestError(
    `Member with userId: ${winner.userId} is not registered on the challenge`
  );
}
const diffWinners = _.differenceWith(filteredWinners, [winner], _.isEqual);
if (diffWinners.length + 1 !== filteredWinners.length) {
  throw new errors.BadRequestError(
    `Duplicate member with placement: ${helper.toString(winner)}`
  );
fork icon44
star icon17
watch icon25

How does lodash.differenceWith work?

lodash.differenceWith is a function in the Lodash library that creates an array of unique values that are not included in the other given arrays, based on a comparator function that is used to compare the elements.

527
528
529
530
531
532
533
534
535
536
}

CQLLibrary.find({ user: req.user.uid, linkedArtifactId: artifactId }, (error, libraries) => {
  if (error) res.status(500).send(error);
  else {
    const nonAuthoringToolExportLibraries = _.differenceWith(
      elmResultsToSave,
      authoringToolExports,
      (a, b) => a.name === b.name
    );
fork icon14
star icon34
watch icon9

+ 2 other calls in file

498
499
500
501
502
503
504
505
506
507
if (!current) return;
const newOriginal = original.toObject();
const newCurrent = current.toObject();
const totalInfringements = newCurrent.length;
let newInfringements = [];
newInfringements = _.differenceWith(
  newCurrent,
  newOriginal,
  (arrVal, othVal) => arrVal._id.equals(othVal._id),
);
fork icon7
star icon7
watch icon14

+ 11 other calls in file

Ai Example

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

const arr1 = [
  { id: 1, value: "a" },
  { id: 2, value: "b" },
  { id: 3, value: "c" },
];
const arr2 = [
  { id: 2, value: "b" },
  { id: 3, value: "d" },
];

const diffArr = _.differenceWith(arr1, arr2, _.isEqual);

console.log(diffArr);
// Output: [{ id: 1, value: 'a' }]

In this example, lodash.differenceWith is used to compare two arrays of objects arr1 and arr2, and return a new array of objects that are present in arr1 but not in arr2. The comparison function used is _.isEqual, which performs a deep comparison between the objects. The output of the function call is an array containing the object { id: 1, value: 'a' }.

15
16
17
18
19
20
21
22
23
24
25
26
27
console.log(difference); // => [1]


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


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


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

+ 15 other calls in file

1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
if (patch.$push) {
  append(patch.$push);
} else if (patch.$pullAll) {
  _.each(patch.$pullAll, function(val, key) {
    cloneOriginalBase(key);
    self.apos.util.set(patch, key, _.differenceWith(self.apos.util.get(patch, key) || [], Array.isArray(val) ? val : [], function(a, b) {
      return _.isEqual(a, b);
    }));
  });
} else if (patch.$pullAllById) {
fork icon540
star icon0
watch icon125

+ 3 other calls in file

84
85
86
87
88
89
90
91
92
93
module.exports.defer               = _.defer;
module.exports.delay               = _.delay;
module.exports.dictionary          = _.dictionary;
module.exports.difference          = _.difference;
module.exports.differenceBy        = _.differenceBy;
module.exports.differenceWith      = _.differenceWith;
module.exports.disjoin             = _.disjoin;
module.exports.div                 = _.div;
module.exports.divide              = _.divide;
module.exports.done                = _.done;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

375
376
377
378
379
380
381
382
383
384
} else {
  addedNodes = _.differenceBy(modifiedNodes, originalNodes, "path");
  addedNodes = ancestorNodes(addedNodes, true);
}

const removedLeafNodes = _.differenceWith(
  originalLeafNodes,
  modifiedNodes,
  (a, b) => a.path === b.path && !nullified(a, b) && !emptied(a, b)
);
fork icon0
star icon0
watch icon0

+ 8 other calls in file

50
51
52
53
54
55
56
57
58
59
60
61
// console.log(result) //[1.2]


// let rs = _.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');
// console.log(rs)


// _.differenceWith(array, [values], [comparator])
// This method is like _.difference except that it accepts comparator which is invoked to compare elements of array to values. 
// The order and references of result values are determined by the first array. 
// The comparator is invoked with two arguments: (arrVal, othVal).

fork icon0
star icon0
watch icon1

+ 11 other calls in file

554
555
556
557
558
559
560
561
562
563
        .filter(newsletter => newsletter.attributes.status !== 'archived')
        .map(newsletter => newsletter.id);
    const incomingNewsletterIds = memberData.newsletters.map(newsletter => newsletter.id);

    newslettersToAdd = _.differenceWith(incomingNewsletterIds, existingNewsletterIds);
    newslettersToRemove = _.differenceWith(existingNewsletterIds, incomingNewsletterIds);
}

// need to maintain archived newsletters; these are not exposed by the members api
const archivedNewsletters = existingNewsletters.filter(n => n.attributes.status === 'archived');
fork icon0
star icon0
watch icon0

+ 35 other calls in file

960
961
962
963
964
965
966
967
968
969
    document.querySelectorAll(".ts-user-name")
  ).map((elm) => elm.innerText);
  return list;
});
if (userList.length > tempList.length) {
  var dif = _.differenceWith(userList, tempList, _.isEqual);
  //   //New Addition
  // console.log("User Joined", " at ", new Date());
  if (dif.length != 0) {
    userJoinedAction(dif);
fork icon0
star icon0
watch icon1

+ 15 other calls in file

40
41
42
43
44
45
46
47
48
49
50
51
console.log('differenceBy2--->', differenceBy2);
//differenceBy2---> [ { x: 2 } ]


//_.differenceWith(array,[values],[comparator])
var objects1 = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
let differenceWith1 = _.differenceWith(objects1, [{ 'x': 1, 'y': 2 }], _.isEqual);
console.log('differenceWith1--->', differenceWith1);
//differenceWith1---> [ { x: 2, y: 1 } ]


//_.drop(array,[n=1])
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)