繁体   English   中英

如何在Javascript的单个地图函数中执行两项操作

[英]How can I perform two operations inside a single map function in Javascript

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

我也使用Puppeteer。

对于存储库列表,我这样做

const data = await page.evaluate(()=>{
        const tds =Array.from(document.querySelectorAll('.explore-content ol li div h3'));
        return tds.map(td => td.textContent);
    });

这给我这样的结果

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

为了得到星星,我还有另一个功能

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);

    });

以这种方式输出

顶级仓库有

    5,304
  ,

    379
  ,
          ,,,,,,

    1,173
  ,

    44

我想将两个方法的输出合并到一个方法中,以便获得如下结果

charlax / professional-programming有5304星。

如何合并datastars方法的输出,或者如何在一个方法中执行两个不同的操作。 我可以在一个map方法中执行两个模拟操作吗? 如果可以,怎么办?

也许这样做更安全:

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');
});

您想“压缩”两个数组的结果,然后在其上进行映射

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}`)
});

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

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;
}

暂无
暂无

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

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