简体   繁体   English

Lodash - 将对象移动到数组中的第一位而不是按另一个属性排序

[英]Lodash - Move object to first place in array than sort by another property

I have an array of object employees我有一组对象员工

在此处输入图片说明

    {
  "emp1": {
    "BusinessPartnerFormattedName": "Aleksandra Lewandowski",
    "EmpRoleCode": "BUP003",
    "EmpRoleType": "Employee",
    "EmployeeID": "E8000",
    "isAssigned" : true,
    "ObjectID": "00163E0E46241ED7A0EA0590D0655967"
  },
  "emp2": {
    "BusinessPartnerFormattedName": "Aleksandra Lewandowski",
    "EmpRoleCode": "BUP003",
    "EmpRoleType": "Employee",
    "EmployeeID": "E8000",
    "isAssigned" : true,
    "ObjectID": "00163E0E46241ED7A0EA0590D0655967"
  },
  "emp3": {
    "BusinessPartnerFormattedName": "Aleksandra Lewandowski",
    "EmpRoleCode": "BUP003",
    "EmpRoleType": "Employee",
    "EmployeeID": "E8000",
    "isAssigned" : false,
    "ObjectID": "00163E0E46241ED7A0EA0590D0655967"
  },
  "emp4": {
    "BusinessPartnerFormattedName": " Lewandowski",
    "EmpRoleCode": "BUP803",
    "EmpRoleType": "Employee",
    "EmployeeID": "BUP803",
    "isAssigned" : false,
    "ObjectID": "00163E0E46241ED7A0EA0590D0655967"
  },
  "emp5": {
    "BusinessPartnerFormattedName": "Aleksandra",
    "EmpRoleCode": "BUP043",
    "EmpRoleType": "Employee",
    "EmployeeID": "BUP043",
    "isAssigned" : false,
    "ObjectID": "00163E0E46241ED7A0EA0590D0655967"
  },
  "emp6": {
    "BusinessPartnerFormattedName": "Eva Log",
    "EmpRoleCode": "BUP0d03",
    "EmpRoleType": "Employee",
    "EmployeeID": "BUP0d03",
    "isAssigned" : false,
    "ObjectID": "00163E0E46241ED7A0EA0590D0655967"
  }
}

I would like to put the employee where EmployeeID is equal to E8000 first in list , than I want the employee where isAssigned is equal to true to follow我想将EmployeeID等于E8000的员工放在列表中的第一位,而不是我希望isAssigned等于true的员工跟随

So far I've achieved this :到目前为止,我已经做到了:

var sortedItems = _.sortBy(items, function(item) {
  return (item.isAssigned === true || item.EmployeeID === "E8000") ? 0 : 1;
});

But how to make sure that the emp with EmployeeID is equal E8000 to will always be on top ?但是如何确保EmployeeID等于E8000的 emp 将始终在最前面?

Not sure why you need lodash on this:不知道为什么你需要 lodash:

items.sort((a,b)=>{
    if(a.EmployeeID !== b.EmployeeID){
        if(a.EmployeeID === 'E8000') return -1;
        if(b.EmployeeID === 'E8000') return 1;
    };

    if(a.isAssigned !== b.isAssigned){
        return a.isAssigned ? -1 : 1;
    }

    //Remaining sort logic...
})

This translates to:这转化为:

"When deciding if a or b should come first, if a and b don't both have the same EmployeeID value, check if either has value E8000 . If ether one does have that value, put that one first. “在决定a还是b应该放在第一位时,如果ab的 EmployeeID 值不同,请检查其中一个是否具有E8000值。如果以太确实具有该值,则将其放在第一位。

Otherwise, check if a and b have the same value for isAssigned , if they don't, put the one that has true as the isAssigned value first.否则,检查ab是否具有相同的isAssigned值,如果它们不相同,则首先将具有true的值作为isAssigned值。

Finally if the prior conditions weren't met, follow whatever logic you want for setting which should go first.最后,如果不满足先验条件,请按照您想要的任何逻辑进行设置。

Of course, you have to fill in whatever sort logic you want to come after that.当然,您必须填写您想要的任何排序逻辑。 For example, if you want to sort alphabetically on BusinessPartnerFormattedName , you'd fill that Remaining sort logic... block with:例如,如果您想按字母顺序对BusinessPartnerFormattedName进行排序,您可以使用以下内容填充Remaining sort logic...块:

if(a.BusinessPartnerFormattedName < b.BusinessPartnerFormattedName) return -1;
if(a.BusinessPartnerFormattedName > b.BusinessPartnerFormattedName) return 1;
return 0;

You chain the wanted sort order.您链接所需的排序顺序。

 var array = [ { name: "E8000 true", EmployeeID: "E8000", isAssigned : true }, { name: "E8000 true", EmployeeID: "E8000", isAssigned : true }, { name: "E8003 false", EmployeeID: "E8003", isAssigned : false }, { name: "E8001 true", EmployeeID: "E8001", isAssigned : true }, { name: "E8002 true", EmployeeID: "E8002", isAssigned : true }, { name: "E8001 false", EmployeeID: "E8001", isAssigned : false } ]; array.sort((a, b) => (b.EmployeeID === 'E8000') - (a.EmployeeID === 'E8000') || b.isAssigned - a.isAssigned ); console.log(array);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

With lodash用 lodash

 var array = [ { name: "E8000 true", EmployeeID: "E8000", isAssigned : true }, { name: "E8000 true", EmployeeID: "E8000", isAssigned : true }, { name: "E8003 false", EmployeeID: "E8003", isAssigned : false }, { name: "E8001 true", EmployeeID: "E8001", isAssigned : true }, { name: "E8002 true", EmployeeID: "E8002", isAssigned : true }, { name: "E8001 false", EmployeeID: "E8001", isAssigned : false } ]; console.log(_.sortBy(array, [ o => o.EmployeeID !== 'E8000', o => !o.isAssigned ]));
 .as-console-wrapper { max-height: 100% !important; top: 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

The data you provided is highly misleading, however I still would like to publish my solution, because basically I just like it.您提供的数据具有很强的误导性,但是我仍然想发布我的解决方案,因为基本上我只是喜欢它。

 const items = [{BusinessPartnerFormattedName:"Aleksandra Lewandowski",EmpRoleCode:"BUP003",EmpRoleType:"Employee",EmployeeID:"E7000",isAssigned:!0,ObjectID:"00163E0E46241ED7A0EA0590D0655967"},{BusinessPartnerFormattedName:"Aleksandra Lewandowski",EmpRoleCode:"BUP003",EmpRoleType:"Employee",EmployeeID:"E6000",isAssigned:!1,ObjectID:"00163E0E46241ED7A0EA0590D0655967"},{BusinessPartnerFormattedName:"Aleksandra Lewandowski",EmpRoleCode:"BUP003",EmpRoleType:"Employee",EmployeeID:"E8000",isAssigned:!1,ObjectID:"00163E0E46241ED7A0EA0590D0655967"}]; const helper = (emp) => emp.EmployeeID === 'E8000' ? 2 : +(!!emp.isAssigned); const r = items.sort((a, b) => helper(b) - helper(a)); console.log(r);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM