0

I am using the following function:

   remove: function (arr, property, num) {
            for (var i in arr) {
                if (arr[i][property] == num)
                    arr.splice(i, 1);
            }
        },

The function works but now that my application uses the _lodash library and is using all modern IE8 and above browsers I am wondering if there's a more efficient way to accomplish the same functionality using _lodash or some other browser native methods.

  • 1
    That function will fail (leave behind matching entries) if the array has two consecutive matching entries. ([Demonstration](http://jsbin.com/OTiZInU/1)) Also, `for-in` isn't for looping through array indexes, it enumerates property names. (More in [this blog post](http://blog.niftysnippets.org/2011/01/myth-of-arrays.html) and [this SO answer](http://stackoverflow.com/questions/9329446/#9329476).) To do this properly, loop backward: `for (var i = arr.length - 1; i >= 0; --i)` – T.J. Crowder Jan 18 '14 at 08:06
  • Thanks for pointing this out. This may have been the cause of some problems in the past that I did not know about. As far as efficiency, do you think there is a more efficient way to do this with lodash or otherwise. If possible I would like to use _lodash as then I could remove the need for this custom function. –  Jan 18 '14 at 08:11

2 Answers2

0

There are a few lodash functions that can help you with this such as filter and remove:

arr = _.remove(arr,function(row){return row[property] == num });

Also note that lodash methods do not alter the original array that you pass in (unlike your example) so you need to do arr = ....

Zaptree
  • 3,763
  • 1
  • 31
  • 26
  • `_.remove` creates a *new* array, it doesn't modify the array in place as the OP's code tries to do. – T.J. Crowder Jan 18 '14 at 08:14
  • I know that T.J. and that is exactly what I said in my comment that it does not alter the original array unlike his comment. None of the lodash methods do that which is more useful in most cases. All the objects in the array will still be the same objects and if you want to change the array you just set it equal to the result. – Zaptree Jan 18 '14 at 17:08
0

I don't believe lodash has anything to help you with this. It has _.remove, but that returns a new array, and you're modifying an existing one.

Note that your existing remove function has a bug: It will fail (leave behind matching entries) if the array has two consecutive matching entries. (Demonstration) Also, for-in isn't for looping through array indexes, it enumerates property names. (More in this blog post and this SO answer.) To do this properly, loop backward; here's a minimal change that fixes it:

remove: function (arr, property, num) {
            for (var i = arr.length - 1; i >= 0; --i) {
                if (arr[i][property] == num)
                    arr.splice(i, 1);
            }
        },

Here's that change in action: http://jsbin.com/OTiZInU/2

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • TJ yes in this case it's important to modify an existing as this array is used as the source for an Angular grid. –  Jan 18 '14 at 08:14
  • Thanks for your help TJ. Any other efficiency improvements you can think of before I accept the question? –  Jan 18 '14 at 08:15
  • @Melina: I don't know AngularJS enough to say. It may be more efficient (or less efficient!) to create a new array and then tell Angular to update the array it's using for the grid; the grid may be smart enough to reuse the DOM objects for the entries that still exist. You'd have to check the docs. – T.J. Crowder Jan 18 '14 at 08:22