Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I've used angular filters to do all kinds of fun formatting, but always on primitive values. For example, filtering numbers into a currency format.

How would I do that same kind of filtering for an array of values? For example

price = 1
prices = [1,2,3]

Assuming currency is the currency filter mentioned in https://docs.angularjs.org/api/ng/filter/currency

{{price | currency}} 

would return $1.00

I would like

{{prices | currency}} 

to return [$1.00,$2.00,$3.00]

how can i write a filter that does that? Or is there a different tool i should be using?

For more background...I'm using this array inside of the angular-selectize plugin. I don't have the option of using ng-repeat on my values.

share|improve this question
    
Well, you could just use ng-repeat to get to the individual values, then the filter will work. Can you give us more information about what you hope to do with the formatted values? IE are you just going to display them? Is there additional processing? ETC – Vlad274 Feb 18 '15 at 22:28
up vote 1 down vote accepted

The currency filter applies to a single value. If you want a filter that applies to an array, you need to add your own, such as:

angular.module('yourModule')
.filter('currenyArray', function($filter) {
  return function(input, uppercase) {
    input = input || [];
    var out = [];
    for (var i = 0; i < input.length; i++) {
      out.push($filter('currency')(input[i]))
    }
    return out;
  };
})

However this will return an array. If you do want to write the array, you need to adapt the function to compute a string instead of an array.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.