久久久久久久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 服務器時的歡迎消息)
主站蜘蛛池模板: 久久久精品高清 | 国产97碰免费视频 | 99re视频在线观看 | 日韩精品一区二区三区中文字幕 | 久久久青草婷婷精品综合日韩 | 91久久 | 欧美乱码精品一区二区三区 | 日韩在线播放一区 | 天天色官网 | 黄色免费在线观看网站 | 中文字幕一区二区三区日韩精品 | 欧美男人天堂 | 亚洲精品乱码久久久久v最新版 | 精品久久久久久久久久久久久久久久久 | 成人毛片视频在线播放 | 青青草社区 | 一区二区三区免费 | 激情一区二区三区 | 亚洲一区二区免费看 | 免费人成激情视频在线观看冫 | 久久中文一区二区 | 天天夜干| 久久av资源网 | 一区二区三区欧美 | 一区二区三区四区免费观看 | 亚洲在线视频 | 日韩亚洲视频 | 久久久久久免费毛片精品 | 欧美激情视频一区二区三区在线播放 | 久久久久久久久久毛片 | 一级免费毛片 | 国产精品九九 | 欧美成年人视频在线观看 | 久久性 | 亚洲精品成人av久久 | 成人伊人| 国产精品日韩欧美一区二区 | 精品久久久久久久久亚洲 | 欧美日韩精品亚洲 | 91免费看片 | 中文字幕一区二区三区精彩视频 |