Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在angularjs中按id数组对对象列表排序?_Javascript_Angularjs_Arrays_Sorting - Fatal编程技术网

Javascript 如何在angularjs中按id数组对对象列表排序?

Javascript 如何在angularjs中按id数组对对象列表排序?,javascript,angularjs,arrays,sorting,Javascript,Angularjs,Arrays,Sorting,我正在尝试对数组列表进行排序,但没有结果 问题:我有一个对象列表,但我需要按数组中的id字符串对其进行排序 JSON: 阵列id序列: ["2613", "2614", "2615", "2616", "2617"] 如何在此数组中对ng repeat或循环进行排序?您可以使用js进行如下排序a.id-b.id var arr=[{“id”:2613,“name”:“Aula 01”,“section”:989},{“id”:2614,“name”:“Aula 02”,“section”:98

我正在尝试对数组列表进行排序,但没有结果

问题:我有一个对象列表,但我需要按数组中的id字符串对其进行排序

JSON:

阵列id序列:

["2613", "2614", "2615", "2616", "2617"]

如何在此数组中对ng repeat或循环进行排序?

您可以使用js进行如下排序
a.id-b.id
var arr=[{“id”:2613,“name”:“Aula 01”,“section”:989},{“id”:2614,“name”:“Aula 02”,“section”:989},{“id”:2616,“name”:“Aula 04”,“section”:989},{“id”:2617,“name”:“Aula 05”,“section”:989,},{“id”:2615,“name”:“3-Aula 03”,“section”:989}]
var res=arr.sort((a,b)=>a.id-b.id)

console.log(res)
使用
ng repeat
进行排序非常简单

<div data-ng-repeat="item in items | orderBy: 'id' ">

您可以使用本机方法和比较函数,如下所示:

var json = [{"id":2613,"name":"Aula 01","section":989},{"id":2614,"name":"Aula 02","section":989},{"id":2616,"name":"Aula 04","section":989},{"id":2617,"name":"Aula 05","section":989,},{"id":2615,"name":"3 - Aula 03","section":989}];

var result = json.sort((a, b) => a.id - b.id)

console.log(result);
$scope.json = service.getJson().sort((a, b) => a.id - b.id);
假设您的JSON来自服务,它可能如下所示:

var json = [{"id":2613,"name":"Aula 01","section":989},{"id":2614,"name":"Aula 02","section":989},{"id":2616,"name":"Aula 04","section":989},{"id":2617,"name":"Aula 05","section":989,},{"id":2615,"name":"3 - Aula 03","section":989}];

var result = json.sort((a, b) => a.id - b.id)

console.log(result);
$scope.json = service.getJson().sort((a, b) => a.id - b.id);
或者可能:

service.getJson().then(function (data) {

   $scope.json = data.sort((a, b) => a.id - b.id);
});

然后您可以在视图中重复它。

如果您需要序列数组中给定的
id
的任意顺序,您可以使用具有顺序的对象进行排序

array=[{id:2613,name:“Aula 01”,section:989},{id:2614,name:“Aula 02”,section:989},{id:2616,name:“Aula 04”,section:989},{id:2617,name:“Aula 05”,section:989},{id:2615,name:“3-Aula 03”,section:989}],
顺序=[“2617”、“2613”、“2614”、“2615”、“2616”],
顺序={};
forEach(函数(id,i){order[id]=i+1});
sort(函数(a,b){返回顺序[a.id]-order[b.id];});
console.log(数组)

.as控制台包装{max height:100%!important;top:0;}
有几种方法可以做到这一点。 1.您可以在控制器中编写一个排序函数,在调用api后对返回对象进行排序,并在排序后放入一个变量以引用angular。
2.你可以这样使用:on ng repeat

如果你问的是angular,为什么这个问题会用PHP标记?你问的是按照另一个数组的顺序对对象数组进行排序,ID按照排序顺序排列?如果这是为了在页面上显示ng repeat,那么你可以使用@Lealcelderio的可能副本,如果这真的是@Lealcelderio的问题,那不是id的顺序,也不是数组及其序列的是。@NinaScholz,你是对的。我误解了这个问题。感谢Adriel Olivera的澄清,我有两个数组,第二个是id序列,不一定是从最小到最大的顺序!我不懂css!如果没有帮助或注释,则序列是按任意顺序排列的,如上文所述,其中第一个id大于后面的所有id,例如,它们是升序的。css只是在stackoverflow上为您提供了一个更大的控制台窗口。