久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

迭代 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 ?

我現(xiàn)在正在使用:

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 中會產(chǎn)生大量開銷.

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

推薦答案

你想要的是對對象或數(shù)組的惰性迭代.這在 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 的節(jié)點模塊來實現(xiàn)迭代器(可能還有生成器).我找不到任何實現(xiàn).您可以查看 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.

您可以嘗試以下方法,但它也會將所有鍵加載到內(nèi)存中

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 是一種原生方法,它可能會帶來更好的優(yōu)化.

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

基準測試

如您所見,Object.keys 明顯更快.實際的內(nèi)存存儲是否更優(yōu)化是另一回事.

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);
});

這篇關(guān)于迭代 node.js 中的對象鍵的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發(fā)出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 在线免费av观看 | 黄色片视频免费 | 视频在线观看一区 | 精品美女 | 中文字幕日韩在线 | 国产成人精品午夜视频免费 | 精品国产一区二区三区av片 | www.蜜桃av| a级大片免费观看 | 成人免费大片黄在线播放 | 新91| 小视频你懂得 | 成人在线中文字幕 | 亚洲国产精品99久久久久久久久 | 精品在线观看入口 | 日韩国产精品一区二区三区 | h片在线免费看 | 狠狠狠色丁香婷婷综合久久五月 | 国产精品久久久久久久久久久久久久 | 黄色一级在线播放 | 国产精品美女久久久久久不卡 | 蜜桃黄网 | 久久精品国产免费 | 男女国产网站 | 成人精品一区二区 | 青青久草 | 在线国产99 | 久久中文字幕电影 | 亚洲国产一区二区在线 | 亚洲五码久久 | 日本三级网址 | 99精品欧美一区二区三区综合在线 | 亚洲欧美国产精品久久 | 日韩中文字幕 | 久久精品色欧美aⅴ一区二区 | 一区二区三区福利视频 | 国产在线拍偷自揄拍视频 | 欧美午夜精品久久久久久浪潮 | 青青草在线播放 | 午夜男人视频 | 久久久蜜桃 |