Search code examples
javascriptangularjsangularjs-service

How to make filterFilter return an array of indexes instead of items?


I want to use filterFilter functionality but I want to reuse the arrays of data instead of creating a new array everytime it runs. So returning an array of indexes and then I can, inside my service, return the items associated to the indexes. This way I can create a $watch export on my service, so I can watch on the real items instead of the filters.

I considered copying the angular filterFilter source, but that isn't maintainable in the long run, what could I do?

Context:

I'm writing a form wizard service that applies a schema to the steps (there are many with slightly different object structures each step), that holds an array of objects. filterFilter returns a new array of objects, so when I modify the object inside the service using a "setter", it's not updating the object itself, but a new (?) created object, and that's no use for me


Solution

  • Javascript has Object.keys( arrayName ) to do just that, but you'll have to include a poly-fill for IE8 or lower.

    var a = ['foo', 'bar'];
    Object.keys( a ); //returns ['0','1'];
    

    The MDN Object.keys article has an article for implementation if you want to use this with older browsers.

    However, upon looking at your problem I think you misread the filter filter. When you use this filter it may return a different array than the input, however every object in that filter is still a reference to an object in the original. So editing any object should modify the original without a problem.

    Hope this helps.