Javascript 对对象数组进行排序,并获取N个元素

Javascript 对对象数组进行排序,并获取N个元素,javascript,arrays,node.js,sorting,lodash,Javascript,Arrays,Node.js,Sorting,Lodash,在NodeJS服务中,我有一个包含具有以下属性的对象的数组: batchType:字符串 batchId:string(是散列) transactionId:string(它是散列) 此数组存储不同类型批次的所有事务 基本上,我需要的是能够从数组中获取N个项,但要遵守某些规则: 从每种批次中至少获取1项 每个batchId至少获取1项 有时数组只能有一种类型的批处理 这是阵列的一个示例: let batchTransactionsArray = [ { batchType: 'type1

在NodeJS服务中,我有一个包含具有以下属性的对象的数组:

  • batchType:字符串
  • batchId:string(是散列)
  • transactionId:string(它是散列)
此数组存储不同类型批次的所有事务

基本上,我需要的是能够从数组中获取N个项,但要遵守某些规则:

  • 从每种批次中至少获取1项
  • 每个batchId至少获取1项
  • 有时数组只能有一种类型的批处理
  • 这是阵列的一个示例:

    let batchTransactionsArray = [
      { batchType: 'type1', batchId: '123', transactionId: 'ffasf23' },
      { batchType: 'type1', batchId: '312', transactionId: '423' },
      { batchType: 'type1', batchId: '123', transactionId: '534' },
      { batchType: 'type1', batchId: '312', transactionId: '86' },
      { batchType: 'type2', batchId: '111', transactionId: '97' },
      { batchType: 'type1', batchId: '312', transactionId: '1945' },
      { batchType: 'type1', batchId: '123', transactionId: '79' },
      { batchType: 'type1', batchId: '312', transactionId: '79' },
      { batchType: 'type3', batchId: '425', transactionId: '1555645' },
      { batchType: 'type1', batchId: '123', transactionId: 'fg5' },
      { batchType: 'type1', batchId: '123', transactionId: 'jkh5' },
      { batchType: 'type1', batchId: '312', transactionId: '53j' },
      { batchType: 'type1', batchId: '111', transactionId: '4545' },
      { batchType: 'type2', batchId: '111', transactionId: '534l' },
      { batchType: 'type1', batchId: '111', transactionId: 'jkg435' },
      { batchType: 'type1', batchId: '111', transactionId: 'gfxg23' },
      { batchType: 'type1', batchId: '111', transactionId: '7asdt' },
      { batchType: 'type1', batchId: '222', transactionId: 'jdsa7' },
      { batchType: 'type3', batchId: '663', transactionId: '12423445' },
      { batchType: 'type1', batchId: '111', transactionId: '89saf6' },
      { batchType: 'type1', batchId: '111', transactionId: '12h3g' },
      { batchType: 'type1', batchId: '111', transactionId: '4h3k2hj' },
      { batchType: 'type3', batchId: '663', transactionId: '145' }
    ];
    
    [{ batchType: 'type1', batchId: '123', transactionId: '534' },
     { batchType: 'type1', batchId: '312', transactionId: '86' },
     { batchType: 'type2', batchId: '111', transactionId: '97' },
     { batchType: 'type2', batchId: '111', transactionId: '534l' },
     { batchType: 'type3', batchId: '663', transactionId: '145' }
    ]
    
    我需要的一个输出示例是(如果我想从数组中获得5个事务):

    对TransactionID进行排序的标准是随机的,没有特定的顺序需要满足

    我尝试了一些lodash函数,比如groupBy和sortBy,但还没有成功

    这是我在玩的一个JSFIDLE:


    我非常感谢您的建议。

    您可以使用lodash做类似的事情:

    let data=[{batchType:'type1',batchId:'123',transactionId:'ffasf23'},{batchType:'type1',batchId:'312',transactionId:'423'},{batchType:'type1',transactionId:'534'},{batchType:'type1',batchId:'312 transactionId:'86'},{batchType:'type2',batchId:'111',transactionId:'97'},{batchType:'type1',batchId:'312',transactionId:'1945'},{batchType:'type1',batchId:'123',transactionId:'79'},{batchType:'type3',batchId:'425',transactionId:'1555645'},{batchType:'type1',batchId:'123',transactionId:'fg5'},{batchType:'type1',batchId:'123',transactionId:'jkh5'},{batchType:'type1',batchId:'312',transactionId:'53j'},{batchType:'type1',batchId:'111',transactionId:'4545'},{batchType:'type2',batchId:'111',transactionId:'jkg435'},{batchType:'type1',batchId:'111',transactionId:'gfxg23'},{batchType:'type1',batchId:'111',transactionId:'7asdt'},{batchType:'type1',batchId:'222',transactionId:'jdsa7'},{batchType:'type3',batchId:'663',transactionId:'12423445,{batchType:'type1',batchId:'111',transactionId:'89saf6'},{batchType:'type1',batchId:'111',transactionId:'12h3g'},{batchType:'type1',batchId:'111',transactionId:'4h3k2hj'},{batchType:'type3',batchId:'663',transactionId:'145'};
    const customTake=(d,n)=>{
    常数roundRobinion=(arr)=>{
    让res=[]
    while(展平(arr).length)
    _.each(arr,x=>x.length?res.push(u.remove(x,(y,i)=>i==0)):空)
    返回展平(res)
    }
    常量组=d(d)
    .orderBy(['batchType','batchId']))
    .groupBy('batchType')
    .mapValues(x=>.values(u.groupBy(x,'batchId'))
    .map(x=>roundRobinion(x))
    .value()
    返回uu.take(roundRobinion(组),n)
    }
    console.log(customTake(数据,3))
    控制台日志(customTake(数据,5))
    控制台日志(customTake(数据,6))
    console.log(customTake(数据,8))

    您可以分步骤执行此操作:

    • 首先添加唯一的batchType元素(这符合第一个标准)
    • 然后添加唯一的batchId元素(这符合第二个标准)
    • 如果还有剩余空间,则用剩余元素填充数组(最多N个元素)
    要检查唯一性,您可以使用
    集合
    。因为您说您使用的是lodash,所以您可以使用
    .groupBy
    batchItem
    对数组进行分组,并使用
    batchId
    从每个组中获取唯一的元素

    让BatchTransactionArray=[
    {batchType:'type1',batchId:'123',transactionId:'ffasf23'},
    {batchType:'type1',batchId:'312',transactionId:'423'},
    {batchType:'type1',batchId:'123',transactionId:'534'},
    {batchType:'type1',batchId:'312',transactionId:'86'},
    {batchType:'type2',batchId:'111',transactionId:'97'},
    {batchType:'type1',batchId:'312',transactionId:'1945'},
    {batchType:'type1',batchId:'123',transactionId:'79'},
    {batchType:'type1',batchId:'312',transactionId:'79'},
    {batchType:'type3',batchId:'425',transactionId:'1555645'},
    {batchType:'type1',batchId:'123',transactionId:'fg5'},
    {batchType:'type1',batchId:'123',transactionId:'jkh5'},
    {batchType:'type1',batchId:'312',transactionId:'53j'},
    {batchType:'type1',batchId:'111',transactionId:'4545'},
    {batchType:'type2',batchId:'111',transactionId:'534l'},
    {batchType:'type1',batchId:'111',transactionId:'jkg435'},
    {batchType:'type1',batchId:'111',transactionId:'gfxg23'},
    {batchType:'type1',batchId:'111',transactionId:'7asdt'},
    {batchType:'type1',batchId:'222',transactionId:'jdsa7'},
    {batchType:'type3',batchId:'663',transactionId:'12423445'},
    {batchType:'type1',batchId:'111',transactionId:'89saf6'},
    {batchType:'type1',batchId:'111',transactionId:'12h3g'},
    {batchType:'type1',batchId:'111',transactionId:'4h3k2hj'},
    {batchType:'type3',batchId:'663',transactionId:'145'}
    ];
    函数getNItems(数组,N=5){
    if(N>=array.length)返回数组;
    让batchTypeGroups=u2;.groupBy(数组,e=>e.batchType),
    res=new Set(),batchIds=new Set();
    //添加唯一的批处理类型
    for(让[batchType,elements]的Object.entries(batchTypeGroups)){
    如果(res.size==N)中断;
    设d=elements.find(e=>!batchId.has(e.batchId));
    如果(d){
    决议增补(d);
    batchId.add(d.batchId);
    }否则{
    res.add(元素[0]);
    }
    }
    //添加剩余的唯一批处理ID
    让batchIdGroups=u.groupBy(array.filter(e=>!batchId.has(e.batchId)),e=>e.batchId);
    for(让Object.entries的[batchId,elements](batchIdGroups)){
    如果(res.size==N)中断;
    res.add(元素[0]);
    }
    //a