减少多个字段排序的 Javascript 代码

我有一个数组,其中的数据与此类似,但有更多字段可供排序:


const shirts= [

    {

      type: "T-shirt",

      size: "L",

      color: "Black",

    },

    {

      type: "Dress Shirt",

      size: "S",

      color: "Brown",

    },

    {

      type: "Sweatshirt",

      size: "M",

      color: "Red",

    }, 

    {

      type: "Polo",

      size: "XS",

      color: "Pink",

    },  

    ...]

我有一个排序功能,根据选择的内容以不同的方式工作,例如,如果用户按尺寸排序,则需要从 XS 到 XL,但任何其他选项都需要按字母顺序排序。这就是我所拥有的:


//sort is the option the user decided to sort by

SortedShirts(sort,shirts){

        var newShirtArray=[];

        var size1=0;

        var size2=0;

        if(sort === "type"){

            newShirtArray= shirts.sort(function(shirt1,shirt2){

                if (shirt1.type> shirt2.type){ 

                    return 1;

                }

                else{

                    return -1;

                } 

            });

        }

        else if(sort === "color"){

            newShirtArray = shirts.sort(function(shirt1,shirt2){

                if (shirt1.color > shirt2.color){ 

                    return 1;

                }

                else{

                    return -1;

                } 

            });

        }

        else if(sort === "size"){

            newShirtArray = shirts.sort(function(shirt1,shirt2){

                if(shirt1.size==="XS"){

                    size1=0;

                }

                else if(shirt1.size==="S"){

                    size1=1;

                }

                else if(shirt1.size==="M"){

                    size1=2;

                }

                else if(shirt1.size==="L"){

                    size1=3;

                }

                else if(shirt1.size==="XL"){

                    size1=4;

                }


                if(shirt2.size==="XS"){

                    size2=0;

                }



这对我来说似乎是重复的,因为类型和颜色以相同的方式排序,只是在不同的字段中,我觉得我可以将其放入一种排序中,但我不确定如何做到这一点。我想知道是否有这样的事情


或者减少我的代码的另一种方法?


万千封印
浏览 97回答 2
2回答

暮色呼如

您可以使用一个switch声明:switch (sort) {&nbsp; &nbsp; case 'color':&nbsp; &nbsp; case 'type':&nbsp; &nbsp; &nbsp; &nbsp; newShirtArray = shirts.sort(/* ... */);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case 'size':&nbsp; &nbsp; &nbsp; &nbsp; newShirtArray = shirts.sort(/* ... */);&nbsp; &nbsp; &nbsp; &nbsp; break;}要将衬衫尺寸转换为数字,您可以使用一个对象:const ShirtSizes = {&nbsp; &nbsp; XS: 0, S: 1, M: 2, L: 3, XL: 4,};const shirt1Size = ShirtSizes[shirt1.size];如果您的环境允许,请使用更简洁的 ES2015 箭头函数:case 'size':&nbsp; &nbsp; newShirtArray = shirts.sort((s1, s2) => ShirtSizes[s1.size] - ShirtSizes[s2.size]);&nbsp; &nbsp; break;对于type和color,正如您所猜测的,您可以使用:case 'color':case 'type':&nbsp; &nbsp; newShirtArray = shirts.sort((s1, s2) => {&nbsp; &nbsp; &nbsp; &nbsp; if (s1[sort] > s2[sort]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; } else if (s1[sort] < s2[sort]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });我希望这会有所帮助。

慕的地6264312

您可以使用括号访问 JSON 对象的密钥。const shirts = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; type: "T-shirt",&nbsp; &nbsp; &nbsp; &nbsp; size: "L",&nbsp; &nbsp; &nbsp; &nbsp; color: "Black",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; type: "Dress Shirt",&nbsp; &nbsp; &nbsp; &nbsp; size: "S",&nbsp; &nbsp; &nbsp; &nbsp; color: "Brown",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; type: "Sweatshirt",&nbsp; &nbsp; &nbsp; &nbsp; size: "M",&nbsp; &nbsp; &nbsp; &nbsp; color: "Red",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; type: "Polo",&nbsp; &nbsp; &nbsp; &nbsp; size: "XS",&nbsp; &nbsp; &nbsp; &nbsp; color: "Pink",&nbsp; &nbsp; },]function SortedShirts(sort, shirts) {&nbsp; &nbsp; var newShirtArray = [];&nbsp; &nbsp; var size1 = 0;&nbsp; &nbsp; var size2 = 0;&nbsp; &nbsp; if (sort === "type" || sort === "color") {&nbsp; &nbsp; &nbsp; &nbsp; newShirtArray = shirts.sort(function (shirt1, shirt2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (shirt1[sort] > shirt2[sort]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; else if (sort === "size") {&nbsp; &nbsp; &nbsp; &nbsp; const sizes = ['XS', 'S', 'M', 'L', 'XL']&nbsp; &nbsp; &nbsp; &nbsp; newShirtArray = shirts.sort(function (shirt1, shirt2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const size1 = sizes.indexOf(shirt1.size)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const size2 = sizes.indexOf(shirt2.size)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (size1 > size2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript