AngularJs - 通过字段的度假村阵列

时间:2017-08-20 07:32:33

标签: javascript angularjs

我有一个这样的数组:

[Object,Object,Object];

每个对象都具有名称为" rate"的属性。我只想用rate属性对这个对象进行排序。

我的JS中有变量($scope.restaurants.data)。这是这个变量的结构:

Array[3]
0:Object
  ID:3
  name:"bestRest"
  profile:"rest.png"
  rate:1
  restaurantCitySlug:"NY"
  slug:"foo"
  __proto__:Object
1:Object
  ID:7
  name:"bestRes3t"
  profile:"rest7.png"
  rate:0
  restaurantCitySlug:"NY"
  slug:"fo4o"
  __proto__:Object
2:Object
  ID:7
  name:"bestR242es3t"
  profile:"re3st7.png"
  rate:2
  restaurantCitySlug:"NY"
  slug:"fo244o"
  __proto__:Object

我的例外是:

Array[3]
0:Object
  ID:7
  name:"bestRes3t"
  profile:"rest7.png"
  rate:0
  restaurantCitySlug:"NY"
  slug:"fo4o"
  __proto__:Object
1:Object
  ID:3
  name:"bestRest"
  profile:"rest.png"
  rate:1
  restaurantCitySlug:"NY"
  slug:"foo"
  __proto__:Object
2:Object
  ID:7
  name:"bestR242es3t"
  profile:"re3st7.png"
  rate:2
  restaurantCitySlug:"NY"
  slug:"fo244o"
  __proto__:Object

3 个答案:

答案 0 :(得分:1)

尝试使用此

sortData = $filter('orderBy')(YOURDATA, 'rate');

答案 1 :(得分:1)

您可以在控制器中使用 $filter orderBy ,如下所示

  $scope.restaurants.data = $filter('orderBy')($scope.restaurants.data, 'rate');

答案 2 :(得分:1)

只需使用普通的Array.prototype.sort

$scope.restaurants.data.sort((a, b) => a.rate - b.rate)