小编bas*_*tej的帖子

添加基于另一个属性的额外类型属性

#1我有一个对象列的类型。列可以是可过滤的或不可过滤的,如果isFilterable是,true则类型Column应要求:filterTypeisTopBarFilter?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)

types discriminated-union typescript

5
推荐指数
1
解决办法
91
查看次数

Javascript - 在 es6 中计算具有许多字段的对象数组中的重复项

我有这样的对象数组。

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)

javascript ecmascript-6

3
推荐指数
1
解决办法
1958
查看次数