Результат loda sh groupBy() должен дать { "withType": [], "withOutType": [] }.
groupBy()
{ "withType": [], "withOutType": [] }
const splittedData = _.groupBy(datas, 'type', 'withOutType');
Можно ли разделить две группы на основе наличия клавиши type в 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>
Ожидаемый результат
Используйте _.partition(), чтобы создать две группы - одну для истинных значений, а другую для ложных. Разрушаем и создаем объект:
_.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; }
Вы можете предоставить функцию в качестве второго аргумента для _.groupBy(), который вернет 'withType', если текущий объект имеет свойство type (проверено с помощью _.has()), или он вернет 'withoutType' если нет:
_.groupBy()
'withType'
_.has()
'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);
Вы можете проверить, существует ли type в объекте, и получить объект, сгруппированный по true или false.
true
false
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);
Я вижу решение, подобное этому:
const splits = [ [...datas.filter(element => element.type)], [...datas.filter(element => !element.type)] ]