如何在 Underscore js - Arrays 中按(特定顺序)设置顺序

我有一个数组,我需要基于 TypeID 的自定义排序顺序 CategoryId:(5,9,14,1,9,3) 的最终输出


        console.log(_.sortBy(arr, 'CategoryID')); 

我可以对 CategoryId 进行排序,但我想按特定顺序按 CategoryID 排序。


<html> 

<head> 

    <script type="text/javascript" src= 

    "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore.js"> 

    </script> 

</head> 

<body> 

    <script type="text/javascript"> 

        var arr = [ 

            {name: 'kim', salary: 4000,CategoryId:3, TypeId:1}, 

            {name: 'shelly', salary: 5000,CategoryId:14, TypeId:1}, 

            {name: 'don', salary: 100,CategoryId:9, TypeId:1}, 

            {name: 'mark', salary: 4500,CategoryId:5, TypeId:2}, 

            {name: 'mark', salary: 4500,CategoryId:5, TypeId:1}, 

          {name: 'joh', salary: 3450,CategoryId:9, TypeId:1}, 

          {name: 'joe', salary: 5100,CategoryId:1, TypeId:1}, 

          {name: 'moore', salary: 5100,CategoryId:14, TypeId:2}, 

          {name: 'wane', salary: 1500,CategoryId:14, TypeId:2}, 

          {name: 'dick', salary: 400,CategoryId:3, TypeId:1}                

        ]; 

        console.log(_.sortBy(arr, 'CategoryId')); 

    </script> 

</body> 

我希望

CategoryId:(5,9,14,1,3) 的结果集特定顺序


最终输出:


const output = [{

  name: 'mark',

  salary: 4500,

  CategoryId: 5,

  TypeId: 1,

}, {

  name: 'joh',

  salary: 3450,

  CategoryId: 9,

  TypeId: 1,

}, {

  name: 'don',

  salary: 100,

  CategoryId: 9,

  TypeId: 1,

}, {

  name: 'joh',

  salary: 3450,

  CategoryId: 9,

  TypeId: 1,

}, {

  name: 'shelly',

  salary: 5000,

  CategoryId: 14,

  TypeId: 1,

}, {

  name: 'joe',

  salary: 5100,

  CategoryId: 1,

  TypeId: 1,

}, {

  name: 'kim',

  salary: 4000,

  CategoryId: 3,

  TypeId: 1,

}, {

  name: 'mark',

  salary: 4500,

  CategoryId: 5,

  TypeId: 2,

}, {

  name: 'moore',

  salary: 5100,

  CategoryId: 14,

  TypeId: 2,

}, {

  name: 'wane',

  salary: 1500,

  CategoryId: 14,

  TypeId: 2,

}];


森林海
浏览 197回答 2
2回答

哔哔one

如果你指定你想要的顺序为数组,你可以排序基于每个类别的指标内数组。var arr = [ {name: 'kim', salary: 4000,CategoryId:3, TypeId:1}, {name: 'shelly', salary: 5000,CategoryId:14, TypeId:1}, {name: 'don', salary: 100,CategoryId:9, TypeId:1}, {name: 'mark', salary: 4500,CategoryId:5, TypeId:2}, {name: 'mark', salary: 4500,CategoryId:5, TypeId:1}, {name: 'joh', salary: 3450,CategoryId:9, TypeId:1}, {name: 'joe', salary: 5100,CategoryId:1, TypeId:1}, {name: 'moore', salary: 5100,CategoryId:14, TypeId:2}, {name: 'wane', salary: 1500,CategoryId:14, TypeId:2}, {name: 'dick', salary: 400,CategoryId:3, TypeId:1} ];const categoryOrder = [5,9,14,1,9,3];const result = [...arr].sort((a,b) => {&nbsp; return a.TypeId === b.TypeId&nbsp; &nbsp; ? categoryOrder.indexOf(a.CategoryId) - categoryOrder.indexOf(b.CategoryId)&nbsp; &nbsp; : a.TypeId - b.TypeId;});console.log(result);

萧十郎

为此,您必须制作自己的自定义排序功能,因为使用 underscore.js 无法实现var tempArray = [&nbsp; &nbsp; {name: 'kim', salary: 4000, CategoryId: 3, TypeId: 1},&nbsp; &nbsp; {name: 'shelly', salary: 5000, CategoryId: 4, TypeId: 1},&nbsp; &nbsp; {name: 'don', salary: 100, CategoryId: 9, TypeId: 1},&nbsp; &nbsp; {name: 'mark', salary: 4500, CategoryId: 5, TypeId: 2},&nbsp; &nbsp; {name: 'mark', salary: 4500, CategoryId: 5, TypeId: 1},&nbsp; &nbsp; {name: 'joh', salary: 3450, CategoryId: 9, TypeId: 1},&nbsp; &nbsp; {name: 'joe', salary: 5100, CategoryId: 1, TypeId: 1},&nbsp; &nbsp; {name: 'moore', salary: 5100, CategoryId: 14, TypeId: 2},&nbsp; &nbsp; {name: 'wane', salary: 1500, CategoryId: 14, TypeId: 2},&nbsp; &nbsp; {name: 'dick', salary: 400, CategoryId: 3, TypeId: 1}]function sort(order, array) {&nbsp; &nbsp; result = []&nbsp; &nbsp; order.forEach(o => {&nbsp; &nbsp; &nbsp; &nbsp; let output = array.filter(element => element.CategoryId === o)&nbsp; &nbsp; &nbsp; &nbsp; // To further sort it by type id we can now use sort&nbsp; &nbsp; &nbsp; &nbsp; output = _.sortBy(output, 'TypeId')&nbsp; &nbsp; &nbsp; &nbsp; result = result.concat(output)&nbsp; &nbsp; })&nbsp; &nbsp; // For output to be this way&nbsp;&nbsp; &nbsp; // TypeID: (1) - order by : 5,9,14,1,9,3, TypeID: 2&nbsp;&nbsp; &nbsp; // Order by : 5,9,14,11,9,3.&nbsp; &nbsp; // we can use groupBy&nbsp; &nbsp; return _.groupBy(result, 'TypeId')}sort([5, 9, 14, 1, 9, 3], tempArray)这回答了你的问题了吗 ?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript