首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Javascript中的单个map函数中执行两个操作

如何在Javascript中的单个map函数中执行两个操作
EN

Stack Overflow用户
提问于 2019-01-22 05:50:20
回答 3查看 753关注 0票数 0

我目前正在尝试从Github的趋势页面和它们拥有的星星中获取所有趋势存储库,并从中创建一个文本文件。URL为

我用的也是木桶。

对于存储库的列表,我做了以下操作

代码语言:javascript
复制
const data = await page.evaluate(()=>{
        const tds =Array.from(document.querySelectorAll('.explore-content ol li div h3'));
        return tds.map(td => td.textContent);
    });

给了我这样的结果

代码语言:javascript
复制
The top repositories are 

    charlax / professional-programming
,

    ssloy / tinyraytracer
,

    komeiji-satori / Dress
,

    ForrestKnight / open-source-cs
,

    hjacobs / kubernetes-failure-stories
,

    osforscience / deep-learning-ocean
,

    alexkimxyz / nsfw_data_scrapper
,

    kamranahmedse / developer-roadmap
,

    typescript-eslint / typescript-eslint
,

    Musish / Musish
,

    MisterBooo / LeetCodeAnimation
,

    yagiz / Bagel
,

    SpaceVim / SpaceVim
,

    antonmedv / fx
,

    pjialin / py12306
,

    braver / programmingfonts
,

    macrozheng / mall
,

    Snailclimb / JavaGuide
,

    schollz / howmanypeoplearearound
,

    flutterchina / flutter-in-action
,

    flutter / flutter
,

    rikschennink / shiny
,

    doocs / advanced-java
,

    MFatihMAR / Awesome-Game-Networking
,

    go-task / task

为了得到星星,我还有一个这样的功能

代码语言:javascript
复制
const stars = await page.evaluate(()=>{
        const stars = Array.from(document.querySelectorAll('.explore-content ol li div:nth-child(4) a'));
        return stars.map(star=>star.textContent);

    });

以这种方式输出的

最高层的回复

代码语言:javascript
复制
    5,304
  ,

    379
  ,
          ,,,,,,

    1,173
  ,

    44

我希望将这两个方法的输出合并到一个方法中,这样我就可以得到如下结果

查拉克斯/专业-节目有5,304颗星。

如何将datastars方法的输出组合起来,或者如何在一个方法中完成两个不同的操作。我可以在一个map方法中执行两个模拟的操作吗?如果是的话,怎么做?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-01-22 22:02:45

也许是一个更安全的方法

代码语言:javascript
复制
const data = await page.evaluate(() => {
  const exctactedData = [];
  for (const entry of document.querySelectorAll('ol.repo-list > li')) {
    exctactedData.push(`${
      entry.querySelector('h3').innerText
    } has ${
      entry.querySelector('a[href$="/stargazers"]').innerText.trim()
    } stars.`);
  }
  return exctactedData.join('\n');
});
票数 1
EN

Stack Overflow用户

发布于 2019-01-22 05:59:52

您希望“压缩”两个数组的结果,然后对其进行映射。

代码语言:javascript
复制
await page.evaluate(()=>{
  const repos = Array.from(document.querySelectorAll('.explore-content ol li div h3'));
  const stars = Array.from(document.querySelectorAll('.explore-content ol li div:nth-child(4) a'));

  // this is an array of tuples (two element arrays)
  // where the first element is the name and the second is the star count
  const zipped = repos.map((repoName, idx) => [repoName, stars[idx])

  return zipped.map(([repoName, starCount]) => `${repoName.textContent} ${starCount.textContent}`)
});
票数 0
EN

Stack Overflow用户

发布于 2019-01-22 06:13:41

你可以做这样的事。你不必为同一件事等两次。

代码语言:javascript
复制
const data = await page.evaluate(()=>{
  const stars = Array.from(document.querySelectorAll('.explore-content ol li div:nth-child(4) a'));
  const tds =Array.from(document.querySelectorAll('.explore-content ol li div h3'));
  var resArr = []
  for(let i = 0; i<stars.length; i++){
    resArr.push(`${tds[i].textContent} has ${stars[i].textContent}`)
  }
  return resArr;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54301991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档