如何通过lodash groupBy()区分带密钥和不带密钥的JSON?

时间:2020-04-28 14:29:42

标签: javascript json ecmascript-6 underscore.js lodash

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:"$3,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:"$1,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:"$2,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:"$2,728.46",picture:"http://placehold.it/32x32",age:37},{_id:"5ea2ecbd297854a2f85909a4",firstName:"Stuart",surname:"Pickett",guid:"322031f9-1a71-44ae-b1cf-cbe7430b78b6",isActive:false,balance:"$1,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:"$2,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": []
}

4 个答案:

答案 0 :(得分:1)

我看到了这样的解决方案:

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

答案 1 :(得分:1)

您可以提供一个函数作为_.groupBy()的第二个参数,如果当前对象具有'withType'属性(使用_.has()检查),则该函数将返回type否则将返回'withoutType'

const data = [{_id:"5ea2ecbde53c090b4ba4fa12",firstName:"Dillon",surname:"Mclaughlin",guid:"a727438f-4ddc-4422-af4a-c7396fda73c3",isActive:true,balance:"$3,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:"$1,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:"$2,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:"$2,728.46",picture:"http://placehold.it/32x32",age:37},{_id:"5ea2ecbd297854a2f85909a4",firstName:"Stuart",surname:"Pickett",guid:"322031f9-1a71-44ae-b1cf-cbe7430b78b6",isActive:false,balance:"$1,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:"$2,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>

答案 2 :(得分:1)

您可以检查对象中是否存在type,并获取按truefalse分组的对象。

const datas=[{_id:"5ea2ecbde53c090b4ba4fa12",firstName:"Dillon",surname:"Mclaughlin",guid:"a727438f-4ddc-4422-af4a-c7396fda73c3",isActive:true,balance:"$3,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:"$1,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:"$2,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:"$2,728.46",picture:"http://placehold.it/32x32",age:37},{_id:"5ea2ecbd297854a2f85909a4",firstName:"Stuart",surname:"Pickett",guid:"322031f9-1a71-44ae-b1cf-cbe7430b78b6",isActive:false,balance:"$1,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:"$2,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>

答案 3 :(得分:1)

使用_.partition()创建两个组-一组代表真实值,另一组代表虚假值。解构并创建对象:

const datas=[{_id:"5ea2ecbde53c090b4ba4fa12",firstName:"Dillon",surname:"Mclaughlin",guid:"a727438f-4ddc-4422-af4a-c7396fda73c3",isActive:true,balance:"$3,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:"$1,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:"$2,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:"$2,728.46",picture:"http://placehold.it/32x32",age:37},{_id:"5ea2ecbd297854a2f85909a4",firstName:"Stuart",surname:"Pickett",guid:"322031f9-1a71-44ae-b1cf-cbe7430b78b6",isActive:false,balance:"$1,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:"$2,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>