在地图实例上使用地图/过滤器行为 [英] Using map/filter behavior on Map instance

查看:65
本文介绍了在地图实例上使用地图/过滤器行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这条快速路线:

app.use('/stop_jobs', function (req, res, next) {

  const jobId = req.query && req.query.job_id;

  const stopped : Array<any> = [];

  for (let j of runningJobs.keys()) {
     if(j.id === jobId){
       stopped.push(j.stopThisInstance());
     }
  }

  res.json({success: {stopped}});

});

其中

 const runningJobs = new Map<CDTChronBase,CDTChronBase>();

(CDTChronBase是一个类)

(CDTChronBase is a class)

以上方法有效,但我想尽可能简化它,像这样:

the above works, but I'd like to simplify it if possible, something like this:

app.use('/stop_jobs', function (req, res, next) {
  const jobId = req.query && req.query.job_id;

  const stopped = runningJobs.keys().filter(j => {
     return j.id === jobId;
  })
  .map(v => {
     return j.stopThisInstance()
  });

  res.json({success: {stopped}});

});

但是对于我来说,我无法弄清楚如何获取Map上任何方法的Array实例.有没有办法在Map对象上使用map/filter类型的方法?

but for the life of me, I cannot figure out how to get an Array instance of any of the methods on Map. Is there a way to use map/filter type methods on Map objects?

推荐答案

由于键和值是相同的对象,因此您可以提取其中之一,然后将其秘密转换为数组.
如果将映射直接转换为数组,则它将为[[key, value],[key, value], ...]结构.然后选择索引0:

Since the key and value are same object, you can extract one of them and then covert it into an array.
If you convert map into array directly, it will be in [[key, value],[key, value], ...] structure. Then pick index 0:

const stopped = Array.from(runningJobs)
  .map(job => job[0])
  .filter(key => key.id === jobId)
  .map(key => key.topThisInstance());

这篇关于在地图实例上使用地图/过滤器行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆