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

时间:2019-08-26 22:09:40

标签: javascript node.js puppeteer discord.js

我有一个Web抓取,但是我搜索一个与我拥有的数组匹配的值和我在抓取中获得的数组,我用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');

2 个答案:

答案 0 :(得分: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 ; 

  });

Ps:请不要使用eval,而应使用JSON.parse(a)来防止JavaScript代码注入。

答案 1 :(得分:0)

为什么不尝试将匹配结果保存在动态数组中而不是返回值,就像全局数组一样?

let array = [];
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
                  array.push(jobMatch);
          }
    }
  }

  });