为了账号安全,请及时绑定邮箱和手机立即绑定

排序后的响应在前端 jquery laravel 中变为未排序

排序后的响应在前端 jquery laravel 中变为未排序

慕容708150 2023-10-14 11:18:34
我在排序后从 Laravel 控制器发送响应。响应已排序。 public function search(Request $request)    {     $providers = Provider::get()->map(function($item) use($request){        $item->setAttribute('distance',$this->addDistanceToProvider($item,$request)) ;          return $item;    })->sortBy('distance');        return $providers;    }回复{"1":{"id":2,"owner_name":"hjjk","owner_email":"bilal@gmail.com","owner_phone":"4567898765432","name":"hgjjj","email":"devyempresas@gmail.com","phone":"","password":"$2y$10$9KmwSmKcxQNHxN6\/KmViQOVgGOIbsTxnPDd.prkTpK9BfVMJ0CXpm","rfc":"12344","tax":"67","location":"Nezahualc\u00f3yotl, State of Mexico, Mexico","longitude":"-98.9896643","latitude":"19.3994934","comission":76,"status":1,"distance":314.703},"2":{"id":3,"owner_name":"abc","owner_email":"asphaltairborne316@gmail.com","owner_phone":"06767672626","name":"Bilal Arshad","email":"swift.solutions.com@gmail.com","phone":"","password":"$2y$10$mLRpt7O2o0wdg8AecgpWA.4xfN9hV4VaG.JRSZyr\/hUoKPjkCkdvm","rfc":"asd","tax":"234","location":"Cuernavaca, Morelos, Mexico","longitude":"-99.22156590000002","latitude":"18.9242095","comission":113,"status":1,"distance":369.312},"0":{"id":1,"owner_name":"bilal","owner_email":"swift.solutions.com@gmail.com","owner_phone":"0213123132131","name":"Bilal Arshad","email":"admin@admin.com","phone":"","password":"$2y$10$BUgsN4Qknk2M\/LNQwQoOrOOad.pu9dFQUiylFSSTgaRrQwDIbDhqG","rfc":"12344","tax":"dasdasd","location":"Nuevo Laredo, Tamaulipas, Mexico","longitude":"-99.68859527909136","latitude":"27.784235863652583","comission":877,"status":1,"distance":1034.684}}但是当我在 brwoser 中看到预览时,它没有排序,并且 Jquery 以未排序的形式呈现数据Jquery函数    $.each(data, function(index, provider) {        embedData(index,provider)        });                                    
查看完整描述

1 回答

?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

由于对象在 javascript 中是未排序的,你可以尝试像这样对它们进行排序


//Assume variable data contains the response data


//convert the object of objects to array of objects 

let arr = Object.keys(data).map((key) => data[key]);


//Sort the array of objects by distance

let sorted = arr.sort((a,b) => a.distance - b.distance);


//use sorted in the function

$.each(sorted, function(index, provider) {

    

    embedData(index,provider)

        

});


查看完整回答
反对 回复 2023-10-14
  • 1 回答
  • 0 关注
  • 79 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信