How to use the difference function from underscore

Find comprehensive JavaScript underscore.difference code examples handpicked from public code repositorys.

underscore.difference returns an array containing all the elements of the first array that are not present in the second array.

106
107
108
109
110
111
112
113
114
115
 */
function mergeArrays(original, newArray) {
    /* jshint camelcase: false */
    const originalKeys = _.pluck(original, 'object_id');
    const newKeys = _.pluck(newArray, 'object_id');
    const addedKeys = _.difference(newKeys, originalKeys);
    const differenceArray = newArray.filter(function (item) {
        return item.object_id && addedKeys.indexOf(item.object_id) >= 0;
    });
    const originalNames = _.pluck(original, 'name');
fork icon74
star icon56
watch icon23

3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
renderedSignals = renderedSignals.concat(newSignals);
renderedSignals = _.intersection(
    renderedSignals,
    allSignals
);
hiddentSignals = _.difference(
    allSignals,
    renderedSignals
);
currentAttributes.rendered = renderedSignals;
fork icon0
star icon2
watch icon3

+ 11 other calls in file

How does underscore.difference work?

The _.difference function in Underscore.js takes two or more arrays as input and returns an array of elements that are present in the first array but not in the other arrays. It creates a new array that contains only the unique elements of the first array that are not present in any of the other arrays. It compares the elements using strict equality (i.e., ===) to determine whether they are the same or not.

2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
  return flatten(array, shallow, []);
};

// Return a version of the array that does not contain the specified value(s).
_.without = function(array) {
  return _.difference(array, slice.call(arguments, 1));
};

// Produce a duplicate-free version of the array. If the array has already
// been sorted, you have the option of using a faster algorithm.
fork icon0
star icon2
watch icon0

3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
// assesmentActionPlan = _.reject(assesmentActionPlan,(acionplanObj)=>{
//     return acionplanObj.isPositive 
// })
// let categotyActionplanIds = _.uniq(_.pluck(assesmentActionPlan,'actionplanId'));
// let customerActionplanIds = _.uniq(_.pluck(customerActionPlan,'actionplanId'));
// actionPlanIds = _.difference(categotyActionplanIds,customerActionplanIds);

// processCustomerActionplan(actionPlanIds,orgId,categoryId,assesmentActionPlan)
// .then(function(data) {
//     clback();
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [4, 5, 6, 7, 8];
const diffArr = _.difference(arr1, arr2);
console.log(diffArr); // Output: [1, 2, 3]

In this example, _.difference takes two arrays arr1 and arr2, and returns a new array containing all the elements from the first array that are not in the second array. The resulting array diffArr contains the elements [1, 2, 3], which are the elements in arr1 that are not in arr2.

478
479
480
481
482
483
484
485
486
487
    cb
  )
// find the dangling doc ids
return task(function (error, projectDocIds) {
  if (error) return callback(error)
  const danglingDocIds = _.difference(allDocIds, projectDocIds)
  logger.debug(
    { allDocIds, allProjectIds, projectDocIds, danglingDocIds },
    'checking for dangling doc ids'
  )
fork icon0
star icon0
watch icon202

+ 5 other calls in file

382
383
384
385
386
387
388
389
390
391
  return result1
})()
const loadedPackIds = Array.from(loadedPacks).map(pack =>
  pack._id.toString()
)
const packIdsToFetch = _.difference(allPackIds, loadedPackIds)
logger.debug(
  { projectId, docId, loadedPackIds, allPackIds, packIdsToFetch },
  'analysed packs'
)
fork icon0
star icon0
watch icon202

+ 2 other calls in file

650
651
652
653
654
655
656
657
658
659

// add: If an address is present on `add`, we propose its addition to the signer list
// remove: If an add is present on `remove`, we propose its removal on the signer list

const add = compare.difference(cluster_signers_1, signer_list),
	  remove = compare.difference(signer_list, cluster_signers_1);

// NPL Round 2
const cluster_signers_2 = await NPLResponse({
	content: JSON.stringify({
fork icon0
star icon0
watch icon1

+ 11 other calls in file