小编bar*_*zek的帖子

使用基于嵌套值的数组过滤对象数组

我试图基于一些嵌套对象过滤数组.我准备了一些小提琴

输入数组如下所示:

let arrayOfElements = 
    [
        {
           "name": "a",
           "subElements": 
           [
             {"surname": 1},
             {"surname": 2}
           ]
        },
        {
           "name": "b",
           "subElements": 
           [
             {"surname": 3},
             {"surname": 1}
           ]
        },
        {
           "name": "c",
           "subElements": 
           [
             {"surname": 2},
             {"surname": 5}
           ]
        }
    ];
Run Code Online (Sandbox Code Playgroud)

我希望这种情况的输出看起来像这样:

let filteredArray = 
    [
        {
          "name": "a",
          "subElements": 
          [
            {"surname": 1}
          ]
        },
        {
          "name": "b",
          "subElements": 
          [
            {"surname": 1}
          ]
        }
];
Run Code Online (Sandbox Code Playgroud)

我正在使用这个公式来做到这一点:

let filteredArray = arrayOfElements.filter((element) => element.subElements.some((subElement) => subElement.surname === 1));
Run Code Online (Sandbox Code Playgroud)

输出几乎是好的,但它返回带有姓氏的所有对象的对象(更好地检查小提琴:D),而不是将它们切掉.我该如何改进过滤?

javascript filtering

12
推荐指数
5
解决办法
2万
查看次数

带延迟测试 NGRX 效果

我想测试一下效果如下:

  1. 如果调度了 LoadEntriesSucces 操作,则效果开始
  2. 等待5秒
  3. 5 秒后发送 http 请求
  4. 当响应到达时,将调度新的操作(取决于响应是成功还是错误)。

Effect 的代码如下所示:

  @Effect()
  continuePollingEntries$ = this.actions$.pipe(
    ofType(SubnetBrowserApiActions.SubnetBrowserApiActionTypes.LoadEntriesSucces),
    delay(5000),
    switchMap(() => {
      return this.subnetBrowserService.getSubnetEntries().pipe(
        map((entries) => {
          return new SubnetBrowserApiActions.LoadEntriesSucces({ entries });
        }),
        catchError((error) => {
          return of(new SubnetBrowserApiActions.LoadEntriesFailure({ error }));
        }),
      );
    }),
  );
Run Code Online (Sandbox Code Playgroud)

我想测试的是5秒后是否发出效果:

it('should dispatch action after 5 seconds', () => {
  const entries: SubnetEntry[] = [{
    type: 'type',
    userText: 'userText',
    ipAddress: '0.0.0.0'
  }];

  const action = new SubnetBrowserApiActions.LoadEntriesSucces({entries});
  const completion = new SubnetBrowserApiActions.LoadEntriesSucces({entries});

  actions$ = hot('-a', { …
Run Code Online (Sandbox Code Playgroud)

rxjs ngrx ngrx-effects jasmine-marbles rxjs-marbles

8
推荐指数
1
解决办法
5846
查看次数

组合框是链接的(这很糟糕)

我正在做简单的WinForms应用程序,我面临一些奇怪的问题.

我的表格:

在此输入图像描述

它很简单:3个组合框和两个按钮 - 确定和取消.

视图:

private void applyOrderButton_Click(object sender, EventArgs e)
    {
        List<string> testList = new List<string>()
        {
            "A",
            "B",
            "C"
        };

        comboBox1st.DataSource = testList;
        comboBox2nd.DataSource = testList;
        comboBox3rd.DataSource = testList;

        comboBox1st.SelectedIndex = 2;
        comboBox2nd.SelectedIndex = 1;
        comboBox3rd.SelectedIndex = 0;
        //Presenter.DoTest();
    }
Run Code Online (Sandbox Code Playgroud)

在校准方法之后会发生什么applyOrderButton_Click()(单击Ok按钮后会发生)我的所有组合框都会更改所选位置.但是,每个组合框都具有相同的选定索引 - 在这种特殊情况下,它将是"A".

然后我使用我的cursour更改了更改comboBox selectedIndex(例如我选择第三个comboBox来显示"C"),对所有三个组合框执行更改.我做错了什么?

c# combobox winforms

5
推荐指数
2
解决办法
157
查看次数

异常处理 - 这是一种好的做法吗?

我想知道我要做的是好事还是坏事.我有那个班:

public class Element : IElement
{
    public float? Max { get; private set; }

    public float? Min { get; private set; }

    public float? Average { get; private set; }

    public bool HasValue { get; private set; }


    public void SetRange(float? min, float? max)
    {
        if (min >= max)
        {
           throw new WrongElementValueException("Min must be greater than max!");
        }
        else if (min < 0f || max < 0f)
        {
           throw new WrongElementValueException("Min/max must be greater than 0!");
        }
        else …
Run Code Online (Sandbox Code Playgroud)

.net c# exception-handling exception

4
推荐指数
1
解决办法
252
查看次数

NGRX - 将选择器与 props 结合起来

当其中一个减速器需要道具时,如何组合减速器?

我有以下型号:

interface Device {
  id: string;
  data: IDeviceData;
}
Run Code Online (Sandbox Code Playgroud)

和 DeviceReducer 如下所示:

import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { Device } from '../model/device';
import { SubnetBrowserApiActions } from 'src/app/explorer/actions';

export interface State extends EntityState<Device> { }

export const adapter: EntityAdapter<Device> = createEntityAdapter<Device>();

export const initialState: State = adapter.getInitialState();

export function reducer(
  state = initialState,
  action:
    | SubnetBrowserApiActions.SubnetBrowserApiActionsUnion
): State {
  switch (action.type) {
    case SubnetBrowserApiActions.SubnetBrowserApiActionTypes.LoadEntriesSucces: {
      return adapter.upsertMany(action.payload.entries, state);
    }

    default: {
      return state;
    }
  } …
Run Code Online (Sandbox Code Playgroud)

ngrx ngrx-store

4
推荐指数
1
解决办法
9577
查看次数