Java分组
-
lombda
分组为List<Object,List<Object>>
list.stream().collect(Collectors.groupingBy(SlhEvaluationAssignment::getTeamId));
- 将集合某个字段作为
Map
的key
,实体本身作为value
,数据格式为Map<Object,Object>
list.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1));
- 获取分组后的值
for (Map.Entry<Integer, List<SlhEvaluationAssignment>> entry : collect.entrySet()) {
Integer teamId = entry.getKey();
List<SlhEvaluationAssignment> slhEvaluationAssignmentList = entry.getValue();
}
JS
- 将数组某个字段作为
{}
的属性
,实体本身作为属性值
转为对象
let data = Object.fromEntries(array.map(item => [item.frequency, item]))
// 取值
data['分组的字段值']
- 前端多字段过滤
/**
* 前端多字段过滤(入参分别为:原数组、检索的值、检索字段如['id','name'])
*/
search(arr, query, key = []) {
return arr.filter(v => Object.values(Object.fromEntries(Object.entries(v).filter(item => (key.length ? key.includes(item[0]) : true)))).some(v => new RegExp(query + '').test(v)))
},