Open In App

Lodash _.xorWith() Method

Improve
Improve
Like Article
Like
Save
Share
Report

Lodash _.xorWith() method is similar to the _.xor() method except that it accepts a 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:

  • [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 return the new array of filtered values.

Example 1: In this example, we are operating xor on the given arrays and printing the results in the console.

javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let objects = [{ 'x': 3, 'y': 4 }, { 'x': 4, 'y': 3 }];
let 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);


Output:

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

Example 2: In this example, we are operating xor on the given arrays and printing the results in the console.

javascript




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


Output:

[ 23, 4, 6, 98 ]

Example 3: In this example, we are operating xor on the given arrays and printing the results in the console.

javascript




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


Output:

[ 's', 'r' ]

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



Last Updated : 03 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads