Tag: 迭代

使用Node.js迭代超过5000万个MongoDB集合logging的最快方法

我正在使用stream()迭代在Node.JS中有超过5500万条logging的集合。 它是一个简单的迭代,我检查每个logging的不同值,如果它们存在或不存在,然后计算有多lesslogging缺less这些值。 迭代复杂度为O(n)。 因此,57万GB大小的5500万条logging需要将近18个小时。 花费太多时间了吗? 我正在使用下面的查询迭代样本是55万logging的集合。 db.collection(`sample`).find({}).stream() .on('data', (data) => { if (data.value) { count++ } }) .on('error', (error) =>{ }) .on('end', () => { console.log('success') }) 迭代所有logging(不使用任何查询或过滤条件)的最佳方法是什么? 我想知道这样的迭代是否合适,如果没有,那么会出现什么问题呢?

如何设置Phantomjs页面抓取的时间间隔

目前我写了一个Phantomjs的脚本,通过多个页面。 我的脚本工作,但我不知道如何设置时间间隔擦伤。 我尝试使用setInterval并传递大约每5秒从arrayList的项目,但它似乎并没有工作。 我的脚本不断打破。 这是我的示例phantomjs脚本代码: 没有setInterval var arrayList = ['string1', 'string2', 'string3'….] arrayList.forEach(function(eachItem) { var webAddress = "http://www.example.com/eachItem" phantom.create(function(ph) { return ph.createPage(function(page) { return page.open(yelpAddress, function(status) { console.log("opened site? ", status); page.injectJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() { setTimeout(function() { return page.evaluate(function() { //code here for gathering data }, function(result) { return result ph.exit(); }); }, 5000); }); }); […]

在Pug / Jade中迭代未知的JSON

我正在用node / express构build我的第一个api,并试图创build一个简单的Jade视图,这个视图将在一堆不同的模型中呈现JSON,所以我不想为每个模型创build不同的视图。 我只是想迭代任何响应,并在表中吐出结果。 有没有一个通用的版本可以做到这一点? 这是我的快递文件中的内容: /* Post a STATE and send to update.jade view*/ router.post('/', function(req, res, next) { State.build(req.body).save() .then(function(State){ res.render("update", { title: "States Post", data: State }); }) }); /* GET all STATES and send to a update.jade view */ router.get('/', function(req, res, next) { State.findAll().then(function(States){ res.render("update", { title: "States Get All", […]

在Pug中迭代多个数组

我正在用Node和Express制作一个图片库应用程序。 我使用帕格作为我的模板引擎。 我有单独的文件夹的全尺寸,图像以及缩略图。 基本上我想要的只是渲染缩略图,并使该缩略图链接到该图像。 我传递一个对象与两个数组从路由,它输出到控制台: { link: [ '/images/20170902_164510.jpg', '/images/IMG-20170903-WA0002.jpg', '/images/IMG-20170903-WA0003.jpg', '/images/IMG-20170903-WA0004.jpg' ], thumb: [ '/thumbnails/20170902_164510.jpg', '/thumbnails/IMG-20170903-WA0002.jpg', '/thumbnails/IMG-20170903-WA0003.jpg', '/thumbnails/IMG-20170903-WA0004.jpg' ] } 我有我的帕格模板如下 each image in images a(href=image.link, data-toggle='lightbox') img(src=image.thumb) 这不会输出任何内容到页面中。 如果我这样做,它的工作 each image in images.thumb a(href=image, data-toggle='lightbox') img(src=image) 但只适用于缩略图的数组。 那么我怎样才能访问这两个数组的适当的方式,我可以分开链接和缩略图? 先谢谢你!

影响Node.js中的callback内的对象的属性

我正在用Node.js编写一个简单的原型,其中有一些帮助器方法,我可能需要使用该原型的对象。 我想要的一个方法是实现jQuery的.each() 。 我在他们的开发版本中查看了jQuery的实现,并试图在我的简化版本中模拟它。 // Loop through the object using a callback BaseProto.prototype.each = function (cb, args) { var obj = this.get(), // Get our object as a plain object prop; /** Code to make sure the the args passed are actually an array **/ if (typeof cb === "function") { // For each property […]

在JavaScript中迭代对象,保持父母

我正在与一个迭代函数战斗。 我有这个对象: var table = { "general": { "amount": ["mandatory", "isAmount"], "order_id": ["mandatory", "isString"] }, "card": { "number": ["Regex:/^[0-9]{16}$/", "mandatory"], "expiry": { "month": ["Regex:/^[0-9]{2}$", "mandatory"], "year": ["Regex:/^[0-9]{4}$/", "mandatory"] }, "network": ["List:maestro|visa|mastercard|amex|jcb", "mandatory"], "cvv": ["Regex:/^[0-9]{3,4}$/", "mandatory"] } } 这里的目标是能够console.log这个: general.amount : ["mandatory", "isAmount"] general.order_id: ["mandatory", "isString"] card.number: ["Regex:/^[0-9]{16}$/", "mandatory"] card.expiry.month: ["Regex:/^[0-9]{2}$", "mandatory"] card.expiry.year: ["Regex:/^[0-9]{4}$/", "mandatory"] card.network: […]

Array.prototype.each = function(callback){for(var i = 0; i <this.length; i ++)callback(this ); } – 这个可以吗?

我知道这是一个味道的问题,但每次我想迭代数组的时候,我的味道使用for循环是坏的。 所以我想出了这个: Array.prototype.each = function(callback) { for (var i = 0; i < this.length; i++) callback(this[i]); } 现在我可以这样做: [10, 20, 30].each(function(n) { console.log(n/10) }) 后来我在互联网上发现了一些提示这种方法的提示,但是我仍然怀疑它是否没有副作用。 这似乎很明显,这是我的担心:) 我没有使用任何类似jQuery或Prototype的库。 我正在为Node.js编码

翡翠列表迭代内有不同的内容

有什么办法来遍历列表,并获得每个列表项中的不同内容? 我现在拥有的是这样的: ul.tabs li.tab-header-and-content a.tab-link(href="") Strategic overview .tab-content .col-4 p Our world is changing fast. Global population is rising rapidly. There is growing pressure on resources. Rising energy prices and climate change have created a need for new lower carbon energy sources. p All of this is driving the transition towards a resource and energy […]