Lodash | _.xor() Method

The _.xor() method returns an array of the symmetric difference of given arrays i.e. it will create an array that contains an element that doesn’t exist in any other array.

Syntax:

_.xor( arrays )

Parameters: This function accepts one or more parameters as mentioned above and described below:

  • arrays: This parameter holds one or more arrays that need to inspect.

Return Value: It returns an array of symmetric difference of given arrays.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

const _ = require('lodash');
  
let x = [3, 10, 100];
  
let y = [100, 10, 2];
  
let symmetricDifference = _.xor(x, y);
  
console.log(symmetricDifference);

chevron_right


Here, const _ = require('lodash') is used to import the lodash library into the file.

Output:

[ 3, 2 ]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

const _ = require('lodash');
  
let x = [3, 10, 100];
  
let y = [100, 10, 2];
  
let z = [10, 500, 3];
  
let symmetricDifference = _.xor(x, y, z);
  
console.log(symmetricDifference);

chevron_right


Output:

[ 2, 500 ]

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

const _ = require('lodash');
  
let js = ['web', 'mobile-app'];
  
let python = ['machine-learning', 'web'];
  
let c = ['basic-programming', 'system-app'];
  
let java = ['mobile-app', 'web', 'basic-programming'];
  
let symmetricDifference = _.xor(js, python, c, java);
  
console.log(symmetricDifference);

chevron_right


Output:

[ 'machine-learning', 'system-app' ]

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

Reference: https://lodash.com/docs/4.17.15#xor

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.