Lodash _.xorWith() Method

The _.xorWith() method is similar to _.xor() method except that it accepts comparator which is invoked to compare elements of arrays. Order of result values which is determined by the order they occur in the arrays.

Syntax:

_.xorWith([arrays], [comparator])

Parameters: This method accepts two parameters as mentioned above and described below:

  • [arrays]: This parameter holds the arrays to inspect.
  • [comparator] (Function): This parameter holds the comparator invoked per element and is invoked with two arguments(arrVal, othVal).

Return Value: This method is used to returns the new array of filtered values.

Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.



javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
  
var objects = [{ 'x': 3, 'y': 4 }, { 'x': 4, 'y': 3 }];
var others = [{ 'x': 3, 'y': 3 }, { 'x': 3, 'y': 4 }];
  
// Use of _.xorWith() 
// method
  
let gfg = _.xorWith(objects, others, _.isEqual);
      
  
// Printing the output 
console.log(gfg);

chevron_right


Output:

[{ x: 4, y: 3 }, { x: 3, y: 3 }]

Example 2:

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
  
var objects = ([ 23, 34, 98 ], [ 34, 23 ]);
var obj = ([ 4, 6 ], [4, 34, 6, 98 ]);
  
// Use of _.xorWith() 
// method
  
let gfg = _.xorWith(objects, obj, _.isEqual);
      
// Printing the output 
console.log(gfg);

chevron_right


Output:

[ 23, 4, 6, 98 ]

Example 3:

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
  
var obj1 = ([ 'p', 'q', 'r' ], [ 'u', 's', 't', 'u' ]);
  
var obj2 = ([ 'p', 'q', 'u', 's' ], [ 't', 'r', 'u' ]);
  
// Use of _.xorWith() method
  
let gfg = _.xorWith(obj1, obj2, _.isEqual);
  
// Printing the output 
console.log(gfg);

chevron_right


Output:

[ 's', 'r' ]

Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.

full-stack-img




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.