-1
votes

Groupe en lodash avec une condition

{
 1: [{ id: 4,
    category_id: 7,
    user_id: 2,
    title: '',
    image1: '15717679702861gohz24vrk2262fov.jpg',
    image2: '15717679702891gohz24vrk2262fox.jpg',
    image3: '15717679702911gohz24vrk2262foz.jpg',
    image4: '15717679702921gohz24vrk2262fp0.jpg',
    image5: '15717679702931gohz24vrk2262fp1.jpg',
    status: 2,
    created_at: 2019-10-22T18:12:50.000Z,
    updated_at: 2019-10-22T18:12:50.000Z,
    item_id: 1,
    category_field_id: 4,
    value: 'Iphone',
    is_title: 1,
    is_description: 0 },
  { id: 3,
    category_id: 7,
    user_id: 2,
    title: '',
    image1: '15717679702861gohz24vrk2262fov.jpg',
    image2: '15717679702891gohz24vrk2262fox.jpg',
    image3: '15717679702911gohz24vrk2262foz.jpg',
    image4: '15717679702921gohz24vrk2262fp0.jpg',
    image5: '15717679702931gohz24vrk2262fp1.jpg',
    status: 2,
    created_at: 2019-10-22T18:12:50.000Z,
    updated_at: 2019-10-22T18:12:50.000Z,
    item_id: 1,
    category_field_id: 3,
    value: 'Other',
    is_title: 0,
    is_description: 0 }
    ]
}

1 commentaires

Qu'avez-vous essayé de faire jusqu'à présent?


3 Réponses :


1
votes
const userInputNumber = 2;
let grouped =_groupBy(obj,(o)=> o.item_id)
Object.keys(grouped).forEach(item_id=> {
 if(grouped[item_id].length !== userInputNumber){
   delete grouped[item_id];
 }
})
You'll have to group first and then check and delete groups which don't have the length of array you desire.

0 commentaires

1
votes

Par défaut, Lodash ne fournit pas la fonctionnalité dont vous avez besoin.

à la place, vous pouvez essayer de combiner _groupby code> et _pickby code> fonctions. P>

var groupedItems = _.groupBy(items, 'item_id');

var userSearchLength = 2;

var filteredGrouping = _.pickBy(groupedItems, function(value, key) {
  return value.length == userSearchLength;
});


0 commentaires

0
votes

J'espère que cela vous aidera.

Vous pouvez d'abord tout d'abord groupe strong> les données, puis strong> les données dont vous aviez besoin. p>

const givenLength = 2;
const groupData =_.groupBy(data,(o)=> o.item_id);
const filtered = _.filter(groupData, (o) => o.length === givenLength );


0 commentaires