第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在 Underscore js - Arrays 中按(特定順序)設(shè)置順序

如何在 Underscore js - Arrays 中按(特定順序)設(shè)置順序

森林海 2021-07-09 15:02:22
我有一個(gè)數(shù)組,我需要基于 TypeID 的自定義排序順序 CategoryId:(5,9,14,1,9,3) 的最終輸出        console.log(_.sortBy(arr, 'CategoryID')); 我可以對(duì) CategoryId 進(jìn)行排序,但我想按特定順序按 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) 的結(jié)果集特定順序最終輸出: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貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果你指定你想要的順序?yàn)閿?shù)組,你可以排序基于每個(gè)類別的指標(biāo)內(nèi)數(shù)組。


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


查看完整回答
反對(duì) 回復(fù) 2021-07-15
?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊

為此,您必須制作自己的自定義排序功能,因?yàn)槭褂?underscore.js 無法實(shí)現(xiàn)


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)

這回答了你的問題了嗎 ?


查看完整回答
反對(duì) 回復(fù) 2021-07-15
  • 2 回答
  • 0 關(guān)注
  • 224 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)