How to use the pullAllBy function from lodash

Find comprehensive JavaScript lodash.pullAllBy code examples handpicked from public code repositorys.

lodash.pullAllBy is a function in the Lodash library that removes all instances of given values from an array based on a specified property.

304
305
306
307
308
309
310
311
312
313
module.exports.pipeline            = _.pipeline;
module.exports.property            = _.property;
module.exports.propertyOf          = _.propertyOf;
module.exports.pull                = _.pull;
module.exports.pullAll             = _.pullAll;
module.exports.pullAllBy           = _.pullAllBy;
module.exports.pullAllWith         = _.pullAllWith;
module.exports.pullAt              = _.pullAt;
module.exports.quaternary          = _.quaternary;
module.exports.rCurry              = _.rCurry;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

93
94
95
96
97
98
99
100
101
102
103
104
105
console.log(pull); // => ['b', 'b']


const pullAll = _.pullAll(['a', 'b', 'c', 'a', 'b', 'c'], ['a', 'c']);
console.log(pullAll); // => ['b', 'b']


const pullAllBy = _.pullAllBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }], [{ 'x': 1 }, { 'x': 3 }], 'x');
console.log(pullAllBy); // => [{ 'x': 2 }]


const pullAllWith = _.pullAllWith([{ 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 }, { 'x': 5, 'y': 6 }], [{ 'x': 3, 'y': 4 }], _.isEqual);
console.log(pullAllWith); // => [{ 'x': 1, 'y': 2 }, { 'x': 5, 'y': 6 }]
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.pullAllBy work?

lodash.pullAllBy works by taking an array, a set of values, and a property as input, and removing all instances of the values from the array based on the specified property.

The function first maps the input array to an array of property values, using the specified property as a key.

It then removes all elements from the array whose property value matches any of the input values using strict equality (===).

lodash.pullAllBy modifies the input array in place and returns the modified array.

For example, if the input array is [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }], the values to remove are [ { x: 2 }, { x: 3 } ], and the property is "x", then the resulting array will be [{ x: 1 }, { x: 4 }].

By using lodash.pullAllBy, developers can easily remove a set of values from an array based on a specified property without the need for writing repetitive filtering logic.

275
276
277
278
279
280
281
282
283
284
285
286
console.log('pullAll1--->', pullAll1);
//pullAll1---> [ 'b', 'b' ]


//_.pullAllBy(array,values,[iteratee=_.identity])
let pullAllByArr = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }];
let pullAllBy1 = _.pullAllBy(pullAllByArr, [{ 'x': 1 }, { 'x': 3 }], 'x');
console.log('pullAllBy1--->', pullAllBy1);
//pullAllBy1---> [ { x: 2 } ]


//_.pullAllWith(array,values,[comparator])
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
const _ = require("lodash");

const array = [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }];
const values = [{ x: 2 }, { x: 3 }];

const result = _.pullAllBy(array, values, "x");

console.log(result); // [{ x: 1 }, { x: 4 }]

In this example, we use lodash.pullAllBy to remove a set of values from an array based on a specified property. We first define an input array array containing four objects with a common property x and a set of values to remove values containing two objects. We then pass the array, values, and "x" as arguments to _.pullAllBy() to remove all instances of the values from the array based on the "x" property. The function returns the modified array, which is then logged to the console. By using lodash.pullAllBy in this way, we can easily remove a set of values from an array based on a specified property without the need for writing repetitive filtering logic.

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)