0

I have created the following filter in angular.js. Now I am trying to use the indexOf to remove the country variable by splice I always get the error saying _filters.indexOf is not a function:

.filter('MyFilter', function () {
    return function (_filters) {
        _filters = _filters.splice(_filters.indexOf(_filters['country']), 1);
    }
})

The filter object looks like this:

{
  "rate": 5,
  "country": [
    "Russia",
    "Polen",
    "France"
  ],
  "city": [
    "Dubai"
  ]
}
GilZ
  • 6,418
  • 5
  • 30
  • 40
jhon dano
  • 660
  • 6
  • 23
  • 3
    Splice is to be used on arrays: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf not objects. – rrd Dec 14 '16 at 07:39
  • 2
    ^ same for `indexOf`. – Cerbrus Dec 14 '16 at 07:39
  • yes! Splice and indexOf are to be used on arrays, if you're trying to remove the country from the object use delete _filters.country; – Nikhilesh Shivarathri Dec 14 '16 at 07:42
  • Can you show, how you want to use that filter? I'm not sure if you understodd the concept of filters. – hansmaad Dec 14 '16 at 08:05
  • @hansmaad: Yes i do understand the concept of filters. Of course the code posted is only a excerpt of the entire filter. Is there a easy way to convert this object into an array? MAybe by using a third party librery like underscre? – jhon dano Dec 14 '16 at 12:15
  • @jhondano normally you should not modify the original object in a filter. that's why i'm asking what you're trying to do. If you give more context, we can give a better answer to your actual problem. – hansmaad Dec 14 '16 at 12:32

1 Answers1

2

Use this code to remove the country property from your filter:

delete obj["country"];
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Rjoydip
  • 66
  • 3
  • [Why you shouldn't use `delete`](http://stackoverflow.com/a/21735614/4927984) – Mistalis Dec 14 '16 at 08:02
  • @Mistalis: so, instead you only "half"-delete it? `= undefined` does not remove the key from the object. – Cerbrus Dec 14 '16 at 08:12
  • `delete` is really slow (**really**). `obj.prop = undefined` "half-delete" a property, but is way faster. – Mistalis Dec 14 '16 at 08:15
  • @Mistalis: _How_ slow? Will I actually _notice_ a difference if I iterate over 10k keys, and delete them all, compared to assigning undefined to all of them? This seems like a premature optimization that doesn 't consider that leaving the keys in there ay have just as much of an performance impact. – Cerbrus Dec 14 '16 at 08:17
  • Take a look to the link I provided, there are some great answers that will explain better than I can do the slow of delete instead of setting `undefined`. – Mistalis Dec 14 '16 at 08:19
  • @Mistalis: The JSPerf link there is broken, but [this benchmark](http://jsben.ch/#/gu1Gi) indicates only a 25% difference in performance, for me. That's very insignificant on a call this fast. – Cerbrus Dec 14 '16 at 08:23
  • 1
    @Mistalis did you really read the link you posted yourself? this answer basically meets the question and is a clean solution. – KarelG Dec 14 '16 at 08:38