Tag: for循环

尝试遍历一个数组并为每个数组添加一个get函数

我想遍历样式标签,并为每个写一个GET函数。 问题是GET函数正在写入'styleTags [i]'的引用,而不是将'styleTags [i]'转换为适当的标签。 var styleTags = ['cont', 'ecce']; for (var i = 0; i < styleTags.length; i++) { app.get('/photos-' + styleTags[i], selectNav, function(req, res) { getDynPhotos(req, res, styleTags[i]); }); }

比较对象数组中的属性值

您好,我正在一个项目中继续学习js到这个URL: http : //themapapp.herokuapp.com/这是github页面: https : //github.com/xtatanx/mapApp 在我的代码的一些部分,我需要检查一些属性是否已经存在于一个对象数组中,而且我的属性值等于某些东西,到目前为止,我正在使用的代码是这样的: // check if property value exist in an array of objects function searchByValue(value, property, array){ for(var i = 0; i < array.length; i++){ if(array[i][property] === value){ return true; } } return false; } 我这样使用它: if(searchByValue('myDestiny', 'id', map.markers)){ map.markers[1].setPosition({ lat: results[0].geometry.location.k, lng: results[0].geometry.location.A }); }else{ createMarker(results[0].geometry.location.k, results[0].geometry.location.A, 'myDestiny'); […]

Javascript:如何在前一个函数的final循环中运行一个函数完成?

我正在编写一个Javascript函数,用于从CloudDB数据库中提取单词,并将它们放入三个数组中的一个:动词,形容词或名词。 一旦所有的单词到位,最后一个循环运行,我想运行下一个function。 代码如下: function verbnoun(array){ verbs = []; adjectives = []; nouns = []; for (i = 0; i<array.length; i++){ //console.log(i); <– returns 0,1,2,3…57,58 as expected my_db.get(array[i], function(err, doc) { if (!err){ if (doc.type == "verb"){ verbs.push(doc.word); } else if (doc.type == "adjective"){ adjectives.push(doc.word); } else if (doc.type == "noun"){ nouns.push(doc.word); } } //console.log(i); <– […]

Javascript for循环等待callback

我有这个function: function tryStartLocalTrendsFetch(woeid) { var userIds = Object.keys(twitClientsMap); var isStarted = false; for (var i = 0; i < userIds.length; i++) { var userId = userIds[i]; var twitClientData = twitClientsMap[userId]; var isWoeidMatch = (woeid === twitClientData.woeid); if (isWoeidMatch) { startLocalTrendsFetch(woeid, twitClientData, function (err, data) { if (err) { // Couldn't start local trends fetch for […]

在JS中做一个for循环等待一个库

如何创build一个for循环等待一个asynchronous调用完成之前开始一个循环的新的迭代没有库(如jQuery)? 例: var items = [1,2,3,4,5]; for (var i = 0; i < items.length; i++) { var promise = new Promise(function(resolve, reject){ asyncAPIcall({ body : item[i] }, function(error, response){ if(error) { reject(); } else { resolve(); } }); promise.then(function() { //continue loop }, function() { //break loop }); } 谢谢 更新(4/29) 我想到了这个解决scheme,我创build了一个自称的函数: var items = […]

在摩卡testing中使用for循环

我是摩卡新手,我想尝试使用for循环来创buildtesting用例。 我想testing一个我input标准12小时时间的函数,并把它输出到24小时军事时间。 这是它的样子。 exports.main = function(time) { var hr = parseInt(time.substr(0,2)); var period = time.substr(8,10); if (period == 'AM' && hr == 12) { hr = '0'; } if (period == 'PM' && hr < 12) { hr += 12; } hr.toString(); if (hr < 10) { hr = '0' + hr; } return time […]

TypeError:比较数组时,无法读取未定义的属性“0”?

我有一个具有JSON值的数组 category = [ {"categoryName":"abcd","productName":"chicken biriyani","price":100,"quantity":"medium"}, {"categoryName":"biriyani","productName":"mutton biriyani","price":130,"quantity":"medium"} ] 我必须检查上述类别数组所需的参数。 所以我创build了一个必需的参数数组。 var reqarray = ['categoryName','productName','price','quantity']; 我创build了用于检查reqarray的值和类别的每个值的示例代码,但它不工作 var categoryList = [ {"categoryName":"abcd","productName":"chicken biriyani","price":100,"quantity":"medium"}, {"categoryName":"biriyani","productName":"mutton biriyani","price":130,"quantity":"medium"} ]; for(var i in categoryList){ var reqarray =['categoryName','productName','price','quantity']; for(var j in arr){ if(categoryList[i].reqarray[j]=='null' || categoryList[i].reqarray[j] == " "){ console.log("data is null"); } else if(categoryList[i].reqarray[j] == undefined){ console.log("required parameters"); } else{ console.log("ok"); […]

为什么我的启动variables在循环运行之前被更改?

所以最奇怪的事情正在发生。 我有这个node.js express服务器,我想运行一个函数,当它启动。 当它开始时,它会查询我的数据库中的一组对象,因为它们太旧,应该删除它们。 我有一个for循环,我想用它来删除它们。 但是,当循环开始,我有一个variables(在这种情况下,它应该被称为现在的迭代器)设置为0, var iterator = 0但它一直设置为我的数组的长度。 所以当for循环开始时,迭代器不等于0,它等于3.然后当循环发生时,它会中断,因为它尝试访问data[3].ChildID; 对于大小为3的数组,而不是data[0].ChildID; 什么可能导致这个? app.listen(3000, function() { console.log("Launch successful. To access app, open your browser and insert the following URL into your address bar: http://localhost:3000/"); // var i = 0; // setInterval(updateDB.callUnenrolledList(function(err, data){ // if(err) { // // do nothing // } // // send the […]

我可以允许在与循环node.js“rest”?

我有一个巨大的for循环,我想在我正在处理时允许I / O继续。 也许每隔一万次左右迭代。 任何方式让我允许这样的额外的I / O?

for循环在nodejs中无法正常工作

我正在使用diffbot API for循环 这是我的代码 for (var i = 0; i< 200; i++){ /* pass url from diffbot */ console.log("Ready to send Url for parsing to Diffbot "); diffbot.article({ uri: url }, function(err, response) { if (!err) { console.log(i); } else { console.log("Error Occur in url " + i + "and error is " + err); […]