gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:19:13 25 4
gpt4 key购买 nike

我有一个网络抓取但是我搜索了一个与我有值的数组和我在抓取中得到的数组的匹配项,我用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com