迭代 node.js 中的对象键

Iterate over object keys in node.js(迭代 node.js 中的对象键)
本文介绍了迭代 node.js 中的对象键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

从 Javascript 1.7 开始,有一个 Iterator 对象,它允许这样做:

Since Javascript 1.7 there is an Iterator object, which allows this:

var a={a:1,b:2,c:3};
var it=Iterator(a);

function iterate(){
    try {  
        console.log(it.next());
        setTimeout(iterate,1000);
    }catch (err if err instanceof StopIteration) {  
        console.log("End of record.
");  
    } catch (err) {  
        console.log("Unknown error: " + err.description + "
");  
    }  

}
iterate();

node.js 中有这样的东西吗?

is there something like this in node.js ?

我现在正在使用:

function Iterator(o){
    /*var k=[];
    for(var i in o){
        k.push(i);
    }*/
    var k=Object.keys(o);
    return {
        next:function(){
            return k.shift();
        }
    };
}

但是通过将所有对象键存储在 k 中会产生大量开销.

but that produces a lot of overhead by storing all the object keys in k.

推荐答案

你想要的是对对象或数组的惰性迭代.这在 ES5 中是不可能的(因此在 node.js 中是不可能的).我们最终会得到这个.

What you want is lazy iteration over an object or array. This is not possible in ES5 (thus not possible in node.js). We will get this eventually.

唯一的解决方案是找到一个扩展 V8 的节点模块来实现迭代器(可能还有生成器).我找不到任何实现.您可以查看 spidermonkey 源代码并尝试使用 C++ 将其编写为 V8 扩展.

The only solution is finding a node module that extends V8 to implement iterators (and probably generators). I couldn't find any implementation. You can look at the spidermonkey source code and try writing it in C++ as a V8 extension.

您可以尝试以下方法,但它也会将所有键加载到内存中

You could try the following, however it will also load all the keys into memory

Object.keys(o).forEach(function(key) {
  var val = o[key];
  logic();
});

然而,由于 Object.keys 是一种原生方法,它可能会带来更好的优化.

However since Object.keys is a native method it may allow for better optimisation.

基准测试

如您所见,Object.keys 明显更快.实际的内存存储是否更优化是另一回事.

As you can see Object.keys is significantly faster. Whether the actual memory storage is more optimum is a different matter.

var async = {};
async.forEach = function(o, cb) {
  var counter = 0,
    keys = Object.keys(o),
    len = keys.length;
  var next = function() {
    if (counter < len) cb(o[keys[counter++]], next);
  };
  next();
};

async.forEach(obj, function(val, next) {
  // do things
  setTimeout(next, 100);
});

这篇关于迭代 node.js 中的对象键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)