按多个条件和可能的未定义值对对象数组进行排序

我正在尝试对我的 javascript 对象数组进行排序


ownerName     dogCode   dogName

Bob           5         Rex

John        

Alisha        3         Moon

Darren        4         Boss  

Josh

Cerq

我希望它首先按dogCode排序(无论是否存在,忽略数字),然后按ownerName排序,最后按dogName排序,如下所示:


ownerName     dogCode   dogName

Alisha        3         Moon

Bob           5         Rex    

Darren        4         Boss 

Cerq

John        

Josh

我试过这个:


data.sort(function (a, b) {

    if (a.dogCode < b.dogCode || !a.dogCode) return 1;

    if (a.dogCode > b.dogCode || !b.dogCode) return -1;

    if (a.ownerName < b.ownerName || !a.ownerName) return 1;

    if (a.ownerName > b.ownerName || !b.ownerName) return -1;

    if (a.dogName < b.dogName || !a.dogName) return 1;

    if (a.dogName > b.dogName || !b.dogName) return -1;

     return 0;

});

显然,它是按dogCode 正确排序的,但不是按名称/dogName 排序的。我怎样才能做到这一点?


编辑:这是我的 json 对象:


{

  "data": [

    {

      "ownerName": "Bob",

      "dogCode": "5",

      "dogName": "Rex"

    },

    {

      "ownerName": "John"

    },

    {

      "ownerName": "Alisha",

      "dogCode": "3",

      "dogName": "Moon"

    },

    {

      "ownerName": "Darren",

      "dogCode": "4",

      "dogName": "Bos"

    },

    {

      "ownerName": "Josh"

    },

    {

      "ownerName": "Cerq"

    }

  ]

}


守着星空守着你
浏览 103回答 2
2回答

慕勒3428872

这可能不是最有效的方法,但我发现下面的代码很容易理解。请注意,排序需要按照与列出它们的顺序相反的顺序进行。const obj = {&nbsp; "data": [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "ownerName": "Bob",&nbsp; &nbsp; &nbsp; "dogCode": "5",&nbsp; &nbsp; &nbsp; "dogName": "Rex"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "ownerName": "John"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "ownerName": "Alisha",&nbsp; &nbsp; &nbsp; "dogCode": "3",&nbsp; &nbsp; &nbsp; "dogName": "Moon"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "ownerName": "Darren",&nbsp; &nbsp; &nbsp; "dogCode": "4",&nbsp; &nbsp; &nbsp; "dogName": "Bos"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "ownerName": "Josh"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "ownerName": "Cerq"&nbsp; &nbsp; }&nbsp; ]};const byPropExists = prop => (a, b) => {&nbsp; if (a[prop] !== undefined && b[prop] === undefined) return -1;&nbsp; if (a[prop] === undefined && b[prop] !== undefined) return 1;&nbsp; return 0;}const byPropValue = prop => (a, b) => a[prop]?.localeCompare(b[prop])// Note the reverse order of your sortsconsole.log(obj.data&nbsp; .sort(byPropValue('dogName'))&nbsp; .sort(byPropValue('ownerName'))&nbsp; .sort(byPropExists('dogCode')));

哔哔one

您可以将未定义检查的增量作为排序值,并按数字增量或String#localeCompare字符串进行排序。const data = [{ ownerName: 'Bob', dogCode: 5, dogName: 'Rex' }, { ownerName: 'John' }, { ownerName: 'Alisha', dogCode: 3, dogName: 'Moon' }, { ownerName: 'Darren', dogCode: 4, dogName: 'Boss' }, { ownerName: 'Josh' }, { ownerName: 'Cerq' }];data.sort((a, b) =>    (a.dogCode === undefined) - (b.dogCode === undefined) ||    (a.ownerName === undefined) - (b.ownerName === undefined) ||    (a.dogName === undefined) - (b.dogName === undefined) ||    (a.ownerName || '').localeCompare(b.ownerName || '') ||    a.dogCode - b.dogCode ||    (a.dogName || '').localeCompare(b.dogName || ''));   console.log(data);.as-console-wrapper { max-height: 100% !important; top: 0; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript