How to use the pullAt function from lodash

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

lodash.pullAt is a function in the Lodash library that removes elements from an array based on their indices.

7
8
9
10
11
12
13
14
15
16

// remove from array
if (pathArray.length) {
  const parent = _.get(object, pathArray);
  if (_.isArray(parent)) {
    return _.pullAt(parent, leavePath);
  }
}

return _.unset(object, path);
fork icon59
star icon212
watch icon8

306
307
308
309
310
311
312
313
314
315
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;
module.exports.random              = _.random;
module.exports.range               = _.range;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

How does lodash.pullAt work?

lodash.pullAt works by taking an array and a set of indices as input and removing the elements at the specified indices from the array.

The function first creates a new array to hold the removed elements and then removes each element from the input array using its index.

lodash.pullAt modifies the input array in place and returns the array of removed elements.

For example, if the input array is [1, 2, 3, 4, 5] and the indices to remove are [1, 3], then the resulting array will be [2, 4], and the modified input array will be [1, 3, 5].

By using lodash.pullAt, developers can easily remove elements from an array based on their indices without the need for writing repetitive code to splice the array.

99
100
101
102
103
104
105
106
107
108
109
110
111
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 }]


const pullAt = _.pullAt(['a', 'b', 'c', 'd'], [1, 3]);
console.log(pullAt); // => ['b', 'd']


const remove = _.remove([1, 2, 3, 4], n => n % 2 === 0);
console.log(remove); // => [2, 4]
fork icon0
star icon4
watch icon0

+ 15 other calls in file

1174
1175
1176
1177
1178
1179
1180
1181
1182
if (req.session.data.prevNames) {
    prevNames = req.session.data.prevNames;
}

if (req.query.item && Number.isInteger(Number(req.query.item)) && prevNames[Number(req.query.item) - 1]) {
    _.pullAt(prevNames, [Number(req.query.item) - 1]);
}

req.session.data.prevNames = prevNames;
fork icon0
star icon0
watch icon1

+ 4 other calls in file

Ai Example

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

const array = [1, 2, 3, 4, 5];
const indices = [1, 3];

const removed = _.pullAt(array, indices);

console.log(removed); // [2, 4]
console.log(array); // [1, 3, 5]

In this example, we use lodash.pullAt to remove elements from an array based on their indices. We first define an input array array containing five elements and an array of indices indices containing two indices. We then pass the array and indices to _.pullAt() to remove the elements at the specified indices from the array. The function returns an array of the removed elements, which is then logged to the console. The array is also modified in place, with the removed elements no longer present, which is also logged to the console. By using lodash.pullAt in this way, we can easily remove elements from an array based on their indices without the need for writing repetitive code to splice the array.

2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
    current_addresses = req.session.data['current_addresses'];
}

if(req.query.type == 'previous'){
    if (req.query.item && Number.isInteger(Number(req.query.item)) && previous_addresses[Number(req.query.item) - 1]) {
        _.pullAt(previous_addresses, [Number(req.query.item) - 1]);
    }
}

if(req.query.type == 'current'){
fork icon0
star icon0
watch icon1

+ 8 other calls in file

287
288
289
290
291
292
293
294
295
296
297
298
console.log('pullAllWith1--->', pullAllWith1);
//pullAllWith1---> [ { x: 1, y: 2 }, { x: 5, y: 6 } ]


//_.pullAt(array,[indexs])
let pullAtArr = ['a', 'b', 'c', 'd'];
let pullAt1 = _.pullAt(pullAtArr, [1, 3]);
console.log('pullAt1--->', pullAt1);
//pullAt1---> [ 'b', 'd' ]


//_.remove(array,[predicate=_.indentity])
fork icon0
star icon0
watch icon0

Other functions in lodash

Sorted by popularity

function icon

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