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

时间:2019-06-24 18:11:34

标签: javascript arrays sorting underscore.js

我有一个数组,我需要基于基于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,
}];

3 个答案:

答案 0 :(得分:1)

为此,您必须创建自己的自定义排序功能,因为使用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)

这能回答您的问题吗?

答案 1 :(得分:1)

如果您将所需订单指定为数组,则可以基于该数组中 中每个类别的索引进行排序。

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);

答案 2 :(得分:0)

访问https://underscorejs.org/#objects

sortBy_.sortBy(列表,iteratee,[上下文]) 返回列表的(稳定)排序副本,并按通过iteratee运行每个值的结果以升序排列。 iteratee也可以是要按其排序的属性的字符串名称(例如,长度)。

Cmd