我需要一个离子3管,排序的id使用OrderArray
的Array1
。
HTML:
< ng-container *ngFor="let item of items | sortBy : ‘id’ >
Array1
[
{“id”: 1,“title”: “Post-1”,“thumb”: “post1.png”},
{“id”: 2,“title”: “Post-2”,“thumb”: “post2.png”},
{“id”: 3,“title”: “Post-3”,“thumb”: “post3.png”},
{“id”: 4,“title”: “Post-4”,“thumb”: “post4.png”},
{“id”: 5,“title”: “Post-5”,“thumb”: “post5.png”}
]
OrderArray
[3, 5, 2, 4, 1]
我需要的结果是:
[
{“id”: 3,“title”: “Post-3”,“thumb”: “post3.png”},
{“id”: 5,“title”: “Post-5”,“thumb”: “post5.png”},
{“id”: 2,“title”: “Post-2”,“thumb”: “post2.png”},
{“id”: 4,“title”: “Post-4”,“thumb”: “post4.png”},
{“id”: 1,“title”: “Post-1”,“thumb”: “post1.png”},
]
希望我能找个人帮忙,我试了很多次都没成功。
谢谢
发布于 2018-12-31 01:31:24
解决方案是:
@Pipe({name: “sortBy”})
export class SortPipe {
transform(array: Array, args: string): Array {
// OrderArray
let sequence = [3, 5, 2, 4, 1];
if (array !== undefined) {
array.sort( function (a, b) {
var A = a[args], B = b[args];
if (sequence.indexOf(A) < sequence.indexOf(B)) {
return 1;
} else {
return -1;
}
});
}
console.log(array);
return array;
}
}
https://stackoverflow.com/questions/53977877
复制相似问题