#1我有一个对象列的类型。列可以是可过滤的或不可过滤的,如果isFilterable是,true则类型Column应要求:filterType,isTopBarFilter?和options(但仅当filterType是'SELECT'- #2 时)。
type Column = {
name: string;
isFilterable: boolean; // passing here false should be equal with not passing the property at all (if possible)
// below properties should exist in type only if isFilterable = true
filterType: 'SELECT' | 'TEXT' | 'DATE';
options: string[]; // this property should exist in type only if filterType = 'SELECT'
isTopBarFilter?: boolean;
}; …Run Code Online (Sandbox Code Playgroud) 我有这样的对象数组。
const array = [ { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 1, y: 2 }, { x: 3, y: 4 }, { x: 1, y: 2 }, { x: 3, y: 12 } ]
Run Code Online (Sandbox Code Playgroud)
我想计算重复对象并将计数存储为新对象字段。
我发现了这个片段,它工作得很好,但它不完全是我需要的。
const names = [{ _id: 1 }, { _id: 1}, { _id: 2}, { _id: 1}]
const result = [...names.reduce( (mp, o) => {
if (!mp.has(o._id)) mp.set(o._id, Object.assign({ count: 0 }, o));
mp.get(o._id).count++;
return mp;
}, …Run Code Online (Sandbox Code Playgroud)