gpt4 book ai didi

javascript - 对对象数组进行排序并取 N 个元素

转载 作者:行者123 更新时间:2023-11-29 23:14:33 24 4
gpt4 key购买 nike

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

  • 批处理类型:字符串
  • batchId: string (是一个hash)
  • transactionId:字符串(它是一个哈希)

这个数组存储了不同批处理类型的所有交易。

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

  1. 至少从每种类型的批处理中获得 1 项
  2. 每个batchId至少得到1个item
  3. 有时数组可能只有一种类型的批处理

这是数组的一个例子:

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' }
];

我需要的输出示例是(如果我想要数组中的 5 个事务):

[{ 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' }
]

transactionIds 的排序标准是随机的,没有特定的顺序要满足。

我正在尝试一些 lodash 函数,例如 groupBy 和 sortBy,但还没有成功。

这是我正在玩的一个 jsfiddle:https://jsfiddle.net/20jh3ze7/

非常感谢您的建议。

最佳答案

你可以使用 lodash 做这样的事情:

let data = [ { 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' } ];

const customTake = (d, n) => {
const roundRobinUnion = (arr) => {
let res = []
while (_.flatten(arr).length)
_.each(arr, x => x.length ? res.push(_.remove(x, (y, i) => i == 0)) : null)
return _.flatten(res)
}
const groups = _(d)
.orderBy(['batchType', 'batchId'])
.groupBy('batchType')
.mapValues(x => _.values(_.groupBy(x, 'batchId')))
.map(x => roundRobinUnion(x))
.value()
return _.take(roundRobinUnion(groups), n)
}

console.log(customTake(data, 3))
console.log(customTake(data, 5))
console.log(customTake(data, 6))
console.log(customTake(data, 8))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

想法是按 batchTypebatchId 进行分组,并将此问题视为循环联合。您遍历每个数组索引并对每个元素进行联合。

如果您关心结束排序顺序,您总是可以在最后执行另一个 orderBy 等。

这是 roundRobinUnion 思想的一个简单示例:

const data = [
[1, 2, 3],
[1],
[5, 6]
]

const roundRobinUnion = (arr) => {
let res = []
while (_.flatten(arr).length)
_.each(arr, x => x.length ? res.push(_.remove(x, (y, i) => i == 0)) : null)
return _.flatten(res)
}

console.log(roundRobinUnion(data)) // [1,1,5,2,6,3]
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

关于javascript - 对对象数组进行排序并取 N 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53175205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com