久久久久久久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 ?

我現在正在使用:

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 中的對象鍵的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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(如何讓我的機器人提及發出該機器人命令的人)
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 服務器時的歡迎消息)
主站蜘蛛池模板: 午夜影院黄 | 国产不卡在线视频 | 91av在线看 | 国产精品一区二区三区不卡 | 超碰在线中文字幕 | 成人伊人网 | 中文字幕永久免费 | 人人干人人爽 | 国产精品久久久久久久成人午夜 | 国产在线视频一区二区 | 日本国产视频 | 香港三日本三级少妇66 | 日韩av一区二区在线观看 | 色av综合 | 欧美日韩一本 | 国产色网站 | 日本不卡一区二区三区 | 在线看片你懂的 | 亚洲影院在线观看 | 欧美激情第二页 | 欧美专区在线观看 | 久久精品视 | 日韩网站在线观看 | 青青青草视频 | 国产91精品看黄网站在线观看 | 日韩精品一区二区视频 | 视频一二区 | 中文字幕一区在线观看 | a视频 | 亚洲va| 国产福利一区二区 | 欧美日韩精品在线 | 一级片免费观看 | 亚洲色欲色欲www在线观看 | 一区二区免费在线观看 | 欧美性精品 | 青青草国产精品 | 一级免费片 | 久久精品欧美一区二区三区不卡 | 日韩天堂网 | 久久免费国产 |