简体   繁体   English

使用函数式编程在 Typescript 中过滤数组 [filter、map、some、reduce 等]

[英]Filter array in Typescript using functional programing [filter, map, some, reduce etc]

I'm trying to work with functional programming我正在尝试使用函数式编程

I have two arrays我有两个数组

arr1=[{prodId:2}{prodId:4}]

arr2=[{id:1, name:"Test1"},
      {id:2, name:"Test2"},
      {id:3, name:"Test3"},
      {id:4, name:"Test4"},
      {id:5, name:"Test5"}]

using a combination of使用组合

filter, map, some, reduce functions过滤器,映射,一些,减少功能

I want to extract items from arr2我想从 arr2 中提取项目

where arr2.id === arr1.prodId 

my output arr would be:我的输出 arr 将是:

  [{id:2, name:"Test2"},
  {id:4, name:"Test4"}]

I'm trying to avoid forEach and use functional programming.我试图避免 forEach 并使用函数式编程。

You should use filter method in combination with includes and map .您应该将filter方法与includesmap结合使用。

 let arr1=[{prodId:2},{prodId:4}], arr2=[{id:1, name:"Test1"}, {id:2, name:"Test2"}, {id:3, name:"Test3"}, {id:4, name:"Test4"}, {id:5, name:"Test5"}]; let ids = arr1.map(({prodId}) => prodId); let result = arr2.filter(({id}) => ids.includes(id)); console.log(result);

Another approach is to use some method.另一种方法是使用some方法。

 let arr1=[{prodId:2},{prodId:4}], arr2=[{id:1, name:"Test1"}, {id:2, name:"Test2"}, {id:3, name:"Test3"}, {id:4, name:"Test4"}, {id:5, name:"Test5"}]; let result = arr2.filter(({id}) => arr1.some(({prodId}) => prodId == id)); console.log(result);

Solutions using .includes , .find , or .some are array operations and cost linear time.使用.includes.find.some解决方案是数组操作和成本线性时间。 When used inside .filter , another linear time operation, the result is a quadratic time computation.当在.filter ,另一个线性时间运算,结果是二次时间计算。 If the input lists are large, this impact is not insignificant.如果输入列表很大,则这种影响并非微不足道。

Instead, first collect the ids to match in a Set then take advantage of constant time lookup in the filter -相反,首先收集要在 Set 中匹配的 id,然后利用filter的恒定时间查找 -

 const arr1 = [ { prodId: 2 } , { prodId: 4 } ] const arr2 = [ { id:1, name:"Test1" } , { id:2, name:"Test2" } , { id:3, name:"Test3" } , { id:4, name:"Test4" } , { id:5, name:"Test5" } ] const find = (whitelist, list) => { const ids = new Set (whitelist.map(x => x.prodId)) // create a set return list.filter(x => ids.has(x.id)) // Set#has uses constant time } console.log(find(arr1,arr2)) // [ { id: 2, name: "Test2" }, { id: 4, name: "Test4" } ]

Is this what you are looking for.这是你想要的。

 arr1=[{prodId:2},{prodId:4}] arr2=[{id:1, name:"Test1"}, {id:2, name:"Test2"}, {id:3, name:"Test3"}, {id:4, name:"Test4"}, {id:5, name:"Test5"}] let k=arr2.reduce((o,a)=>{ if(arr1.map(a=>a.prodId).indexOf(a.id)!=-1) { o.push(a) } return o; },[]) console.log(k)

暂无
暂无

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

相关问题 使用[过滤器,映射,一些,化简等]从2d数组的子对象创建1d数组 - Create 1d array from 2d array's sub objects using [filter, map, some, reduce etc] 在JavaScript中使用带有reduce的map来过滤数组中的对象 - Using map with reduce in javascript to filter objects in an array 懒惰map,JavaScript中的filter,reduce等 - Lazily map, filter, reduce, etc. in JavaScript 如何使用 js 数组方法(例如 map()、reduce()、filter() 等格式化来自服务器的响应,如下所示? - How can I format the response coming from the server as given below using js array methods such as map(), reduce(), filter() and etc? 使用 map 从嵌套数组打印,在 javascript 中减少和过滤 - Print from nested array using map, reduce and filter in javascript 使用map,reduce或filter按属性获取唯一值数组javascript - get unique values array javascript by property using map, reduce or filter 链接数组方法(过滤器、map、reduce)而不是使用双循环 - Chaining array methods (filter, map, reduce) instead of using double loop 是否有一个JavaScript库,它将缺少的标准迭代方法(filter,map,reduce,some ...)添加到Array中? - Is there a JavaScript library that adds missing standard iteration methods (filter, map, reduce, some…) to Array? 使用compose函数和Array.reduce进行JavaScript函数编程 - JavaScript functional programing with compose function and Array.reduce 使用Reduce而不是链接Filter和Map - Using Reduce instead of chaining Filter and Map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM