[英]angular-js filter to find an empty string
I'd like to use the filters to return all values that have an empty string for a particular field. 我想使用过滤器返回特定字段具有空字符串的所有值。
That is, in the code: 也就是说,在代码中:
var groups = $filter('unique')(addresses, 'country');
var groupedByCountry = [];
angular.forEach(groups, function (value, key) {
var selectedGroup = value['country'];
var grouped = $filter('filter')(addresses, { country: selectedGroup });
this.push(grouped);
}, groupedByCountry);
As can be seen in http://jsfiddle.net/b7cjM/ , it creates groups as expected where a country is specified, but I'd like the last group to contain only the addresses that have no country specified (instead of, as currently, a group of all addresses in existence). 如http://jsfiddle.net/b7cjM/所示 ,它会按预期在指定国家/地区的位置创建组,但是我希望最后一个组仅包含未指定国家/地区的地址(而不是当前,存在所有所有地址的组)。
Is this possible using angularjs? 使用angularjs可以做到吗?
** **
As suggested by punund, I wrote a 'groupBy' filter [a naive conversion of the 'unique' filter: http://jsfiddle.net/b7cjM/1 ], which is a much simpler solution to my problem than the one I was attempting. 正如punund建议的那样,我写了一个'groupBy'过滤器['unique'过滤器的简单转换: http : //jsfiddle.net/b7cjM/1 ],它比我以前的解决方案要简单得多。尝试。 I suspect a custom 'filterByEmpty' of this type is also the solution for the problem as presented.
我怀疑这种类型的自定义“ filterByEmpty”也可以解决上述问题。
underscore
or lodash
may work better for you: underscore
或lodash
可能更适合您:
groupedBy = _.groupBy($scope.addresses, 'country');
Empty strings are properly taken into account, and you don't need the second filter. 空字符串已得到适当考虑,您不需要第二个过滤器。 Another option would be to implement a kind of "groupBy" angular filter yourself.
另一种选择是自己实现一种“ groupBy”角度滤波器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.