angularjs在contoller中过滤一个字符串值为整数的数组

时间:2022-12-29 20:13:09

I'm using AngularJS filter to filter an array of objects inside a controller. These objects all have a quality that is a string. I want to be able to filter the array from largest to smallest. But I can't get it filter on value of the integer inside the string.

我正在使用AngularJS过滤器来过滤控制器内的对象数组。这些对象都具有字符串的质量。我希望能够从最大到最小过滤数组。但我无法过滤字符串中整数的值。

array = [{
  object1 {
    value = "3"
  }, {
 object2 {
    value = "1"
  }, {
  object3 {
    value = "22"
  }]

 $filter('orderBy')( array, 'value', true );

This would order the array as: object2, object3, object1. I've tried using parseInt but that doesn't work. Does anyone know the correct syntax?

这会将数组排序为:object2,object3,object1。我尝试过使用parseInt,但这不起作用。有谁知道正确的语法?

1 个解决方案

#1


1  

Replace value with a function that returns the length of the string:

使用返回字符串长度的函数替换值:

$filter('orderBy')( array, function(item) { return parseInt( item.value ); } , true );

#1


1  

Replace value with a function that returns the length of the string:

使用返回字符串长度的函数替换值:

$filter('orderBy')( array, function(item) { return parseInt( item.value ); } , true );