javascript - 为什么我的 for 循环只返回 1 个值

标签 javascript node.js puppeteer discord.js

我有一个网络抓取但是我搜索了一个与我有值的数组和我在抓取中得到的数组的匹配项,我用 for 循环迭代这些数组事情是我只有 1 个值当数组中有多个匹配项时,我想获取所有值,而不仅仅是第一个匹配项。

我的代码。

        let dataJobs = await page.evaluate(()=>{
            var a = document.getElementById("task-listing-datatable").getAttribute("data-tasks"); //Search job list
            var ar = eval(a);//Convert string to arr
            var keyword = ['Image Annotation', 'What Is The Best Dialogue Category About Phones', 'Label Roads( With Sidewalks) In Images']; //This is the list where I'll find match 
          for(let i=0; i<ar.length; i++){ //hago la busqueda de coincidencia
            for(let j=0; j<keyword.length; j++){
                if(ar[i][1] === keyword[j]){
                  let jobMatch =`${ar[i][0]} - ${ar[i][1]} - Paga: ${ar[i][3]} - Numero de Tasks: ${ar[i][5]} @everyone`; //return the Match
                          return jobMatch;
                  }
            }
          }
          
          });

这里是所有的代码:

const puppeteer = require('puppeteer');
const Discord = require('discord.js');
const client = new Discord.Client();
const url = 'url';
var coincidence = [];

(async () => {
    const URL = url
    const browser = await puppeteer.launch({
        'args' : [
          '--no-sandbox',
          '--disable-setuid-sandbox'
        ]
      });
    const page = await browser.newPage()
    await page.goto(URL, { 'waitUntil' : 'networkidle2' });
      console.log("Primer coincidence " + coincidence);
    client.on('message', async message =>{ //When the word start is written, run this:
        if(message.channel.id === '613553889433747477'){
            if(message.content.startsWith('start')) {
                let dataJobs = await page.evaluate(()=>{
                    var a = document.getElementById("task-listing-datatable").getAttribute("data-tasks"); //Search job list
                    var ar = eval(a);//Convert string to arr
                    var keyword = ['Image Annotation', 'What Is The Best Dialogue Category About Phones', 'Label Roads( With Sidewalks) In Images']; //This is the list where I'll find match 
                  for(let i=0; i<ar.length; i++){ //search the coincidence
                    for(let j=0; j<keyword.length; j++){
                        if(ar[i][1] === keyword[j]){
                          let jobMatch =`${ar[i][0]} - ${ar[i][1]} - Paga: ${ar[i][3]} - Numero de Tasks: ${ar[i][5]} @everyone`; //return the Match
                                  return jobMatch;
                          }
                    }
                  }
                  
                  });
                
                console.log(dataJobs);
                console.log(`==== first login ====`)
                console.log(`==================`)

                          if(!coincidence.includes(dataJobs)){ //If there is no coincidence, send the message
                            client.channels.get(`613573853565681671`).send(dataJobs);
                            coincidence.push(dataJobs);
                          }else{//else do not send it 
                            console.log("It was sent")
                          }
            }
        }
        await page.reload();
    })    
    

    
})()

client.on('message', (message)=>{
    if(message.content == '!interval'){
        setInterval(()=>{
            client.channels.get(`613553889433747477`).send(`start`);
        },10000);
    }
});

client.login('token');

我只得到我想得到的所有值中的 1 个值

最佳答案

请检查这个

  let dataJobs = await page.evaluate(()=>{
      var a = document.getElementById("task-listing-datatable").getAttribute("data-tasks"); //Search job list
      var ar = eval(a);//Convert string to arr
      var keyword = ['Image Annotation', 'What Is The Best Dialogue Category About Phones', 'Label Roads( With Sidewalks) In Images']; //This is the list where I'll find match 
      let ret = [] ;
      ar.forEach(val => { if (keyword.includes(val[1])) { ret.push(`${val[0]} - ${val[1]} - Paga: ${val[3]} - Numero de Tasks: ${val[5]} @everyone`)} } )    

      return ret ; 

  });

注意:请不要使用 eval,而是使用 JSON.parse(a) 来防止 javascript 代码注入(inject)。

关于javascript - 为什么我的 for 循环只返回 1 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57665512/

相关文章:

javascript - 如何重新加载并等待元素出现?

javascript - 在 puppeteer 的 headless true 模式下找不到选择器的 Node

javascript - 在不使用return或嵌套if的情况下以reject结束promise函数,这怎么可能?

javascript - 当我不访问某个网站时,是否会为该网站指定 Chrome 扩展程序?

javascript数组到格式化字符串

javascript - Next js 后端默认是多线程的吗?

javascript - node.js 在异步请求中缺少发布数据

javascript - 模板文字不适用于查询选择器

javascript - 如何在 Express 中从一个域重定向到另一个域?

javascript - 使用 Supertest 和模拟数据库进行 Express JS 集成测试