【问题标题】:Filter array of objects by array of ids按 id 数组过滤对象数组
【发布时间】:2018-02-17 18:20:14
【问题描述】:

我有一个包含服务 ID 的数组 activeIds,还有一个包含服务对象的数组 servicesList。

示例:-

activeIds = [202, 204]
serviceList = [{  
                "id":201,
                "title":"a"
               },
               {  
                "id":202,
                "title":"a"
               },
               {  
                "id":203,
                "title":"c"
               },
               {  
                "id":204,
                "title":"d"
               },
               {  
                "id":205,
                "title":"e"
               }];

我想要所有 id 不属于第一个数组的服务(obj),即activeIds。从上面的示例代码 我想要 id 为 201,203,205 的服务 obj

最终输出 -

expectedArray = [{  
                "id":201,
                "title":"a"
               },
               {  
                "id":203,
                "title":"c"
               },
               {  
                "id":205,
                "title":"e"
               }];

这是我的编码尝试。但这根本不正确。请帮忙-

    const activeIds = e; // [202, 204]
    const obj = [];
    this.serviceList.map((s: IService) => {
        activeIds.map((id: number) => {
            if (id !== s.id) {
                obj.push(s);
            }
        });
    });

【问题讨论】:

  • 如果 id 数组很长,我建议您将其转换为对象查找或映射,以避免在数组中对每个元素进行线性搜索。否则,使用 array.filter 函数就像下面每个答案中建议的那样。

标签: javascript arrays angular object


【解决方案1】:

您可以简单地使用 array.filter 和 indexOf 来检查下一个数组中的匹配元素。

var arr = serviceList.filter(item => activeIds.indexOf(item.id) === -1);

演示

let activeIds = [202, 204]
let serviceList = [{  
                "id":201,
                "title":"a"
               },
               {  
                "id":202,
                "title":"a"
               },
               {  
                "id":203,
                "title":"c"
               },
               {  
                "id":204,
                "title":"d"
               },
               {  
                "id":205,
                "title":"e"
               }];


let  arr = serviceList.filter(function(item){
      return activeIds.indexOf(item.id) === -1;
    });
    
console.log(arr);

【讨论】:

  • 如果serviceList真的很大,这个性能如何?
【解决方案2】:

您可以使用filter 和includes 方法来做到这一点。

const activeIds = [202, 204]
const serviceList = [{"id":201,"title":"a"},{"id":202,"title":"a"},{"id":203,"title":"c"},{"id":204,"title":"d"},{"id":205,"title":"e"}]

const result = serviceList.filter(({id}) => !activeIds.includes(id));
console.log(result)

【讨论】:

  • op使用箭头函数,为什么不应该包含work?
  • 一开始他并没有这个问题!或者我可能错过了:(
  • 很好,我需要相反的,但只是省略了!在activeIds 前面是我的解决方案。先生,请投赞成票!
【解决方案3】:

使用filter & indexOf 方法。indexOf 将检查当前 id 是否存在于activeIds 数组中

var activeIds = [202, 204]
var serviceList = [{
    "id": 201,
    "title": "a"
  },
  {
    "id": 202,
    "title": "a"
  },
  {
    "id": 203,
    "title": "c"
  },
  {
    "id": 204,
    "title": "d"
  },
  {
    "id": 205,
    "title": "e"
  }
];

var filteredArray = serviceList.filter(function(item) {
  return activeIds.indexOf(item.id) === -1

});

console.log(filteredArray)

【讨论】:

  • 跟上面贴的不一样
  • 我也有同样的疑问。似乎我们几乎同时发布了
【解决方案4】:

您可以结合indexOf 来检查当前id是否在活动数组上并过滤数组。

【讨论】:

    猜你喜欢
    • 2015-07-20
    • 2021-08-27
    • 1970-01-01
    • 2018-11-12
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多