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

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

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

森林海 2021-07-09 15:02:22
我有一个数组,我需要基于 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,}];
查看完整描述

2 回答

?
哔哔one

TA贡献1854条经验 获得超8个赞

如果你指定你想要的顺序为数组,你可以排序基于每个类别的指标内数组。


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) => {

  return a.TypeId === b.TypeId

    ? categoryOrder.indexOf(a.CategoryId) - categoryOrder.indexOf(b.CategoryId)

    : a.TypeId - b.TypeId;

});


console.log(result);


查看完整回答
反对 回复 2021-07-15
?
萧十郎

TA贡献1815条经验 获得超13个赞

为此,您必须制作自己的自定义排序功能,因为使用 underscore.js 无法实现


var tempArray = [

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

    {name: 'shelly', salary: 5000, CategoryId: 4, 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}

]


function sort(order, array) {

    result = []

    order.forEach(o => {

        let output = array.filter(element => element.CategoryId === o)

        // To further sort it by type id we can now use sort

        output = _.sortBy(output, 'TypeId')

        result = result.concat(output)

    })

    // For output to be this way 

    // TypeID: (1) - order by : 5,9,14,1,9,3, TypeID: 2 

    // Order by : 5,9,14,11,9,3.

    // we can use groupBy

    return _.groupBy(result, 'TypeId')

}


sort([5, 9, 14, 1, 9, 3], tempArray)

这回答了你的问题了吗 ?


查看完整回答
反对 回复 2021-07-15
  • 2 回答
  • 0 关注
  • 196 浏览
慕课专栏
更多

添加回答

举报

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