如何通过lodash groupBy()区分JSON based with key 和without key?

How to differentiate with JSON based with key and without key by lodash groupBy()?

lodash groupBy() 的结果应该是 { "withType": [], "withOutType": [] }

const splittedData = _.groupBy(datas, 'type', 'withOutType');

是否可以根据 JSON 中 type 键的存在来区分为两个组?

const datas=[{_id:"5ea2ecbde53c090b4ba4fa12",firstName:"Dillon",surname:"Mclaughlin",guid:"a727438f-4ddc-4422-af4a-c7396fda73c3",isActive:true,balance:",331.33",picture:"http://placehold.it/32x32",age:37,type:"student"},{_id:"5ea2ecbd067a8ad1cb1f9e62",firstName:"Fleming",surname:"Austin",guid:"e228bbea-ef6b-4709-995c-bf485fa3665c",isActive:true,balance:",869.92",picture:"http://placehold.it/32x32",age:35,type:"teacher"},{_id:"5ea2ecbd0d1a074b255a1349",firstName:"Kaufman",surname:"Alford",guid:"176fb7fe-b7bc-4574-8887-4252a042b24a",isActive:true,balance:",163.34",picture:"http://placehold.it/32x32",age:29,type:"student"},{_id:"5ea2ecbd1162ee44119aa0d3",firstName:"Sykes",surname:"Petty",guid:"92b13963-7d32-469c-be80-c88728a7842a",isActive:false,balance:",728.46",picture:"http://placehold.it/32x32",age:37},{_id:"5ea2ecbd297854a2f85909a4",firstName:"Stuart",surname:"Pickett",guid:"322031f9-1a71-44ae-b1cf-cbe7430b78b6",isActive:false,balance:",725.25",picture:"http://placehold.it/32x32",age:25,type:"student"},{_id:"5ea2ecbd6ef4a9bee37a223a",firstName:"Berg",surname:"Hewitt",guid:"ab05bed9-29e4-4570-aa12-fead954417b0",isActive:false,balance:",935.72",picture:"http://placehold.it/32x32",age:31}];

const splittedData = _.groupBy(datas, 'type', 'noneType');
console.log(splittedData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

预期结果

{
  "withType": [],
  "withOutType": []
}

我看到这样的解决方案:

const splits = [
    [...datas.filter(element => element.type)],
    [...datas.filter(element => !element.type)]
]

您可以提供一个函数作为 _.groupBy() 的第二个参数,如果当前对象具有 type 属性(选中使用 _.has()),否则它将 return 'withoutType' 如果没有:

const data = [{_id:"5ea2ecbde53c090b4ba4fa12",firstName:"Dillon",surname:"Mclaughlin",guid:"a727438f-4ddc-4422-af4a-c7396fda73c3",isActive:true,balance:",331.33",picture:"http://placehold.it/32x32",age:37,type:"student"},{_id:"5ea2ecbd067a8ad1cb1f9e62",firstName:"Fleming",surname:"Austin",guid:"e228bbea-ef6b-4709-995c-bf485fa3665c",isActive:true,balance:",869.92",picture:"http://placehold.it/32x32",age:35,type:"teacher"},{_id:"5ea2ecbd0d1a074b255a1349",firstName:"Kaufman",surname:"Alford",guid:"176fb7fe-b7bc-4574-8887-4252a042b24a",isActive:true,balance:",163.34",picture:"http://placehold.it/32x32",age:29,type:"student"},{_id:"5ea2ecbd1162ee44119aa0d3",firstName:"Sykes",surname:"Petty",guid:"92b13963-7d32-469c-be80-c88728a7842a",isActive:false,balance:",728.46",picture:"http://placehold.it/32x32",age:37},{_id:"5ea2ecbd297854a2f85909a4",firstName:"Stuart",surname:"Pickett",guid:"322031f9-1a71-44ae-b1cf-cbe7430b78b6",isActive:false,balance:",725.25",picture:"http://placehold.it/32x32",age:25,type:"student"},{_id:"5ea2ecbd6ef4a9bee37a223a",firstName:"Berg",surname:"Hewitt",guid:"ab05bed9-29e4-4570-aa12-fead954417b0",isActive:false,balance:",935.72",picture:"http://placehold.it/32x32",age:31}];

const splittedData = _.groupBy(data, o => _.has(o, 'type') ? 'withType' : 'withoutType'); 
console.log(splittedData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

您可以检查对象中是否存在 type 并获取按 truefalse.

分组的对象

const datas=[{_id:"5ea2ecbde53c090b4ba4fa12",firstName:"Dillon",surname:"Mclaughlin",guid:"a727438f-4ddc-4422-af4a-c7396fda73c3",isActive:true,balance:",331.33",picture:"http://placehold.it/32x32",age:37,type:"student"},{_id:"5ea2ecbd067a8ad1cb1f9e62",firstName:"Fleming",surname:"Austin",guid:"e228bbea-ef6b-4709-995c-bf485fa3665c",isActive:true,balance:",869.92",picture:"http://placehold.it/32x32",age:35,type:"teacher"},{_id:"5ea2ecbd0d1a074b255a1349",firstName:"Kaufman",surname:"Alford",guid:"176fb7fe-b7bc-4574-8887-4252a042b24a",isActive:true,balance:",163.34",picture:"http://placehold.it/32x32",age:29,type:"student"},{_id:"5ea2ecbd1162ee44119aa0d3",firstName:"Sykes",surname:"Petty",guid:"92b13963-7d32-469c-be80-c88728a7842a",isActive:false,balance:",728.46",picture:"http://placehold.it/32x32",age:37},{_id:"5ea2ecbd297854a2f85909a4",firstName:"Stuart",surname:"Pickett",guid:"322031f9-1a71-44ae-b1cf-cbe7430b78b6",isActive:false,balance:",725.25",picture:"http://placehold.it/32x32",age:25,type:"student"},{_id:"5ea2ecbd6ef4a9bee37a223a",firstName:"Berg",surname:"Hewitt",guid:"ab05bed9-29e4-4570-aa12-fead954417b0",isActive:false,balance:",935.72",picture:"http://placehold.it/32x32",age:31}];

const splittedData = _.groupBy(datas, o => 'type' in o ? "withType" : "withOutType");
console.log(splittedData);
.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>

使用 _.partition() 创建两组 - 一组用于真值,另一组用于假值。解构,创建对象:

const datas=[{_id:"5ea2ecbde53c090b4ba4fa12",firstName:"Dillon",surname:"Mclaughlin",guid:"a727438f-4ddc-4422-af4a-c7396fda73c3",isActive:true,balance:",331.33",picture:"http://placehold.it/32x32",age:37,type:"student"},{_id:"5ea2ecbd067a8ad1cb1f9e62",firstName:"Fleming",surname:"Austin",guid:"e228bbea-ef6b-4709-995c-bf485fa3665c",isActive:true,balance:",869.92",picture:"http://placehold.it/32x32",age:35,type:"teacher"},{_id:"5ea2ecbd0d1a074b255a1349",firstName:"Kaufman",surname:"Alford",guid:"176fb7fe-b7bc-4574-8887-4252a042b24a",isActive:true,balance:",163.34",picture:"http://placehold.it/32x32",age:29,type:"student"},{_id:"5ea2ecbd1162ee44119aa0d3",firstName:"Sykes",surname:"Petty",guid:"92b13963-7d32-469c-be80-c88728a7842a",isActive:false,balance:",728.46",picture:"http://placehold.it/32x32",age:37},{_id:"5ea2ecbd297854a2f85909a4",firstName:"Stuart",surname:"Pickett",guid:"322031f9-1a71-44ae-b1cf-cbe7430b78b6",isActive:false,balance:",725.25",picture:"http://placehold.it/32x32",age:25,type:"student"},{_id:"5ea2ecbd6ef4a9bee37a223a",firstName:"Berg",surname:"Hewitt",guid:"ab05bed9-29e4-4570-aa12-fead954417b0",isActive:false,balance:",935.72",picture:"http://placehold.it/32x32",age:31}];

const [withType, withoutType] = _.partition(datas, 'type');
console.log({ withType, withoutType });
.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>