【问题标题】:typescript- filter array of objects based on another arraytypescript- 基于另一个数组过滤对象数组
【发布时间】:2019-10-03 15:54:06
【问题描述】:

我有一个对象数组,如下所示

  readonly allItems = [
    {
      id: 0,
      title: "Item 0",
      belongsTo: 'admin'
    },
    {
      id: 1,
      title: "Item 1",
      belongsTo: 'user'
    },
    {
      id: 2,
      title: "Item 2",
      belongsTo: 'all'
    },
    {
      id: 3,
      title: "Item 3",
      belongsTo: 'user'
    },
    {
      id: 4,
      title: "Item 4",
      belongsTo: 'all'
    }
  ];

我有一个如下所示的数字数组

let selItems = [0,2,4];

我要做的是,根据selItems 数组过滤allItems 数组 为此,我编写了以下代码,这显然是错误的。

  for(let i=0; i< this.allItems.length; i++){
      if(selItems.includes(this.allItems[i].id)){
        tempMenu.push(this.allItems[i]);
      }
      console.log(tempMenu);
    }

我得到以下输出

[{
      id: 0,
      title: "Item 0",
      belongsTo: 'admin'
}]

我期待的结果是这样的:

  [
    {
      id: 0,
      title: "Item 0",
      belongsTo: 'admin'
    },
    {
      id: 2,
      title: "Item 2",
      belongsTo: 'all'
    },
    {
      id: 4,
      title: "Item 4",
      belongsTo: 'all'
    }
  ]

谁能告诉我这样做的正确方法? 谢谢!

【问题讨论】:

    标签: javascript arrays typescript for-loop


    【解决方案1】:

    您可以改用.map

    const allItems = [{
        id: 0,
        title: "Item 0",
        belongsTo: 'admin'
      },
      {
        id: 1,
        title: "Item 1",
        belongsTo: 'user'
      },
      {
        id: 2,
        title: "Item 2",
        belongsTo: 'all'
      },
      {
        id: 3,
        title: "Item 3",
        belongsTo: 'user'
      },
      {
        id: 4,
        title: "Item 4",
        belongsTo: 'all'
      }
    ];
    const selItems = [0, 2, 4];
    
    const output = selItems.map(num => allItems.find(({ id }) => id === num));
    console.log(output);

    要将计算复杂度降低到O(N) 而不是O(N^2),可以先将其转换为以id 为索引的对象:

    const allItems = [{
        id: 0,
        title: "Item 0",
        belongsTo: 'admin'
      },
      {
        id: 1,
        title: "Item 1",
        belongsTo: 'user'
      },
      {
        id: 2,
        title: "Item 2",
        belongsTo: 'all'
      },
      {
        id: 3,
        title: "Item 3",
        belongsTo: 'user'
      },
      {
        id: 4,
        title: "Item 4",
        belongsTo: 'all'
      }
    ];
    const selItems = [0, 2, 4];
    
    const allItemsById = allItems.reduce((a, item) => {
      a[item.id] = item;
      return a;
    }, {});
    
    const output = selItems.map(num => allItemsById[num]);
    console.log(output);

    或者filter:

    const allItems = [{
        id: 0,
        title: "Item 0",
        belongsTo: 'admin'
      },
      {
        id: 1,
        title: "Item 1",
        belongsTo: 'user'
      },
      {
        id: 2,
        title: "Item 2",
        belongsTo: 'all'
      },
      {
        id: 3,
        title: "Item 3",
        belongsTo: 'user'
      },
      {
        id: 4,
        title: "Item 4",
        belongsTo: 'all'
      }
    ];
    const selItemsSet = new Set([0, 2, 4]);
    
    const output = allItems.filter(({ id }) => selItemsSet.has(id));
    console.log(output);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-12
      • 1970-01-01
      • 2018-04-04
      • 2021-07-07
      • 2019-11-02
      • 2021-11-21
      • 1970-01-01
      • 2016-03-28
      相关资源
      最近更新 更多