Comma Separated

angular

前端 未结 7 1060
清歌不尽
清歌不尽 2020-12-31 17:25

I have a pretty basic scenario (somewhat new to angular). I am trying to convert JSON:

[
    {\'name\': \'Nick\'},
    {\'name\': \'David\'},
    {\'name\':         


        
7条回答
  •  一整个雨季
    2020-12-31 18:01

    One thing that would be helpful is creating a "map" filter, like so:

    myModule.filter('map', function() {
      return function(input, propName) {
        return input.map(function(item) {
          return item[propName];
        });
      };
    });
    

    That's the simple case of mapping to a named property, you could make it more fancy and understand dot notation, etc. Now with this, in your view you can do:

    {{(menuItems | map:'name').join(',')}}

    Because the map filter returns an Array, which has a built-in join method already in Javascript.

提交回复
热议问题