Return matched array items and remove them from original array

Marko

I'm trying to find whether lodash has a function where I can filter based on some query, return an array of the matched objects, but remove the matched objects from the original array.

So very similar to _.filter, but where the original array is modified with the matched elements removed.

Example

var originalArray = [1, 2, 3, 4, 5];
console.log(originalArray);
----> 1, 2, 3, 4, 5

var evenNumbers = _.somethingSimilarToFilter(originalArray, function(n) {
    return n % 2 === 0
});

console.log(evenNumbers);
----> 2, 4

console.log(originalArray);
----> 1, 3, 5
Pranav C Balan

You can done it using native JavaScript Array#filter and Array#splice methods

var originalArray = [1, 2, 3, 4, 5];
var evenNumbers = originalArray.filter(function(n, i, arr) {
  // just remove the element from array if  even number
  return n % 2 === 0 && arr.splice(i, 1)
});

console.log(originalArray, evenNumbers);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP - Remove items from an array with given parameter

From Dev

KnockoutJS: Remove array of items from an array

From Dev

Is it possible to remove items from a array?

From Dev

remove items from array with array_filter or array_walk

From Dev

Remove items from array

From Dev

remove specific id from array of checked items

From Dev

How to return just the matched elements from a mongoDB array

From Dev

Remove items from one array if not in the second array

From Dev

Remove matched item from array of objects?

From Dev

Remove string characters from a string if not matched in an array

From Dev

How to remove element from array if values matched?

From Dev

Hide elements from Array Adapter, not remove them

From Dev

Remove items from based of substring in array jquery

From Dev

How to remove multiple items from a swift array?

From Dev

Remove items from multidimensional array in PHP

From Dev

KnockoutJS: Remove array of items from an array

From Dev

Remove set of items from an array jQuery

From Dev

push items into array and return them in javascript

From Dev

Remove items from array to ensure no duplicate strings

From Dev

remove matched elements from the array in jquery

From Dev

Remove items from array without looping

From Dev

Delete items from array and later get back them in new array

From Dev

Remove string characters from a string if not matched in an array

From Dev

AngularJS remove items from array compared by id

From Dev

How to remove multiple items from a swift array?

From Dev

Excel to return matched value in rows from a column array

From Dev

Remove multiple items from array based on matched value

From Dev

Remove matched characters from array values if it's on last position of sentance

From Dev

Iterating JS array of objects and returning matched objects as well removing it from the original array?

Related Related

  1. 1

    PHP - Remove items from an array with given parameter

  2. 2

    KnockoutJS: Remove array of items from an array

  3. 3

    Is it possible to remove items from a array?

  4. 4

    remove items from array with array_filter or array_walk

  5. 5

    Remove items from array

  6. 6

    remove specific id from array of checked items

  7. 7

    How to return just the matched elements from a mongoDB array

  8. 8

    Remove items from one array if not in the second array

  9. 9

    Remove matched item from array of objects?

  10. 10

    Remove string characters from a string if not matched in an array

  11. 11

    How to remove element from array if values matched?

  12. 12

    Hide elements from Array Adapter, not remove them

  13. 13

    Remove items from based of substring in array jquery

  14. 14

    How to remove multiple items from a swift array?

  15. 15

    Remove items from multidimensional array in PHP

  16. 16

    KnockoutJS: Remove array of items from an array

  17. 17

    Remove set of items from an array jQuery

  18. 18

    push items into array and return them in javascript

  19. 19

    Remove items from array to ensure no duplicate strings

  20. 20

    remove matched elements from the array in jquery

  21. 21

    Remove items from array without looping

  22. 22

    Delete items from array and later get back them in new array

  23. 23

    Remove string characters from a string if not matched in an array

  24. 24

    AngularJS remove items from array compared by id

  25. 25

    How to remove multiple items from a swift array?

  26. 26

    Excel to return matched value in rows from a column array

  27. 27

    Remove multiple items from array based on matched value

  28. 28

    Remove matched characters from array values if it's on last position of sentance

  29. 29

    Iterating JS array of objects and returning matched objects as well removing it from the original array?

HotTag

Archive