The _.xorBy method is similar to _.xor() method except that it accepts iteratee which is invoked for each element of each arrays to generate the criterion by which they are compared. Order of result values which is determined by the order they occur in the arrays.
Syntax:
_.xorBy(arrays, iteratee)
Parameters: This method accepts two parameters as mentioned above and described below:
- arrays: This parameter holds the arrays to inspect.
- iteratee: This parameter holds the iteratee invoked per element and is invoked with one argument (value).
Return Value: This method returns the new array of filtered values.
Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.
javascript
// Requiring the lodash library const _ = require( "lodash" ); // Original array var x = _.xorBy([3.1, 1.3, 4.6], [3.3, 3.4], Math.floor); var y = _.xorBy([4.1, 2.3, 7.6], [4.3, 7.4], Math.floor); // Use of _.xorBy() method let gfg = _.xorBy(x); let gfg2 = _.xorBy(y); // Printing the output console.log(gfg); console.log(gfg2); |
Output:
[ 1.3, 4.6 ] [ 2.3 ]
Example 2:
javascript
// Requiring the lodash library const _ = require( "lodash" ); // Original array var x = _.xorBy([{ 'x' : 11 }], [{ 'x' : 12 }, { 'x' : 11 }], 'x' ); var y = _.xorBy([{ 'x' : 11.2 }], [{ 'x' : 12.2 }, { 'x' : 56.3 }, { 'x' : 38.21 }, { 'x' : 11.2 }], 'x' ); // Use of _.xorBy() method // The `_.property` iteratee shorthand let gfg = _.xorBy(x); let gfg3 = _.xorBy(y); // Printing the output console.log(gfg); console.log(gfg3); |
Output:
[ { x: 12 } ] [ { x: 12.2 }, { x: 56.3 }, { x: 38.21 }]
Example 3:
javascript
// Requiring the lodash library const _ = require( "lodash" ); // Original array var x = _.xorBy([ 'p' ], [ 'q' , 'r' , 's' , 'p' ]); var y = _.xorBy([ 'tee' ], [ 'tee' , 'cee' , 'dee' , 'bee' , 'eee' ]); // Use of _.xorBy() method // The `_.property` iteratee shorthand let gfg = _.xorBy(x); let gfg3 = _.xorBy(y); // Printing the output console.log(gfg); console.log(gfg3); |
Output:
['q', 'r', 's' ] ['cee', 'dee', 'bee', 'eee' ]
Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.