What is easy way to convert object in array Angular JS?

前端 未结 3 1441
失恋的感觉
失恋的感觉 2021-01-05 07:30

My application was built on Angular JS and has a lot AJAX requests on server. For example, in PHP I format output array like as:

$dialog[$us         


        
3条回答
  •  Happy的楠姐
    2021-01-05 07:42

    orderBy is a filter, and filters require an array. So a straightforward solution is to combine it with a toArray filter. Supposing you want to order your objects using their time property:

    1. {{value.name_user}} said {{value.message}} on {{value.time|date}}

    As the toArray filter is not shipped with AngularJS, you can define one as:

    angular.module('ToArrayDemo', [])
      .filter('toArray', function() {
        return function(obj) {
          const result = [];
          angular.forEach(obj, function(val) {
            result.push(val);
          });
         return result;
       }
     });
    

    See https://plnkr.co/edit/E857InfrEGqToW0SvwOh?p=preview

提交回复
热议问题