简体   繁体   English

按字符串数组过滤对象数组

[英]Filter array of objects by array of strings

My Object:我的 Object:

const searchFor = ['appInfo', 'questions'];

My Data:我的数据:

const data = [
  { id: '1', data: 'jobInfo' },
  { id: '2', data: 'appInfo' },
  { id: '3', data: 'documents' },
  { id: '4', data: 'questions' },
];

I am expecting the final result to be:我期待最终的结果是:

[
  { id: '2', data: 'appInfo' },
  { id: '4', data: 'questions' },
];

My try was that I can filter with one item with fixed value我的尝试是我可以过滤一个具有固定值的项目

const result = Object.keys(searchFor).map((key) => data.filter((obj) => obj.data === key))

I getting array of arrays but I need array of object and more over I not sure if there is any better way to do this.我得到array of arrays但我需要array of object我不确定是否有更好的方法来做到这一点。

Thanks in advance:)提前致谢:)

You may use Array.prototype.filter() together with Array.prototype.includes() :您可以将Array.prototype.filter()Array.prototype.includes() ) 一起使用:

 const searchFor = ['appInfo', 'questions'], data = [{id:'1',data:'jobInfo'},{id:'2',data:'appInfo'},{id:'3',data:'documents'},{id:'4',data:'questions'},], result = data.filter(({data:d}) => searchFor.includes(d)) console.log(result)
 .as-console-wrapper{min-height:100%;}

You could take a Map for the key/objects pairs and map the found objects.您可以将Map用于密钥/对象对,将 map 用于找到的对象。

 const searchFor = ['appInfo', 'questions'], data = [{ id: '1', data: 'jobInfo' }, { id: '2', data: 'appInfo' }, { id: '3', data: 'documents' }, { id: '4', data: 'questions' }], result = searchFor.map( Map.prototype.get, new Map(data.map(o => [o.data, o])) ); console.log(result);

You can use the Array methods .filter and .includes (because searchFor is an Array.)您可以使用数组方法.filter.includes (因为searchFor是一个数组。)

 const searchFor = ['appInfo', 'questions']; const data = [ { id: '1', data: 'jobInfo' }, { id: '2', data: 'appInfo' }, { id: '3', data: 'documents' }, { id: '4', data: 'questions' }, ]; const result = data.filter(item => searchFor.includes(item.data)); console.log(result);

You can do this:你可以这样做:

 const searchFor = ['appInfo', 'questions']; const data = [ { id: '1', data: 'jobInfo' }, { id: '2', data: 'appInfo' }, { id: '3', data: 'documents' }, { id: '4', data: 'questions' }, ]; var res = data.filter(i=> searchFor.includes(i.data)) console.log(res)
If you wanna reduce the complexity to O(N), you may consider this: 如果你想将复杂度降低到 O(N),你可以考虑这个:

 const searchFor = ['appInfo', 'questions']; const data = [ { id: '1', data: 'jobInfo' }, { id: '2', data: 'appInfo' }, { id: '3', data: 'documents' }, { id: '4', data: 'questions' }, ]; var hashTable = {}; searchFor.forEach(i=> hashTable[i] = true ); var res = data.filter(i=>{ return hashTable[i.data] }); console.log(res)

You can use:您可以使用:

const results = data.filter(item => searchFor.includes(item.data)):
console.log(results):
 const result = data.filter((rec, id) => {
  if (searchFor.indexOf(rec.data) > -1) {
    return rec
  }
})
console.log(data.filter(element=>{
   return searchFor.includes(element.data)
}));

The idea here is you have to take elements of data and then have to filter each element of data after matching with search for object.since search for the object that contains your filter criteria so it has to be nested within external iteration ie filter method we have passed on data object.这里的想法是您必须获取数据元素,然后必须在与搜索 object 匹配后过滤每个数据元素。因为搜索包含您的过滤条件的 object,因此它必须嵌套在外部迭代中,即我们的过滤方法已传递数据 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM