node.js에서 객체 키를 반복
Javascript 1.7부터 Iterator 객체가 있습니다.
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.\n");
} catch (err) {
console.log("Unknown error: " + err.description + "\n");
}
}
iterate();
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
.
원하는 것은 객체 또는 배열에 대한 게으른 반복입니다. ES5에서는 불가능합니다 (따라서 node.js에서는 불가능합니다). 우리는 결국 이것을 얻을 것입니다.
유일한 해결책은 반복기 (및 아마도 발전기)를 구현하기 위해 V8을 확장하는 노드 모듈을 찾는 것입니다. 구현을 찾을 수 없습니다. spidermonkey 소스 코드를보고 C ++에서 V8 확장으로 작성해보십시오.
다음을 시도해 볼 수도 있지만 모든 키를 메모리에로드합니다.
Object.keys(o).forEach(function(key) {
var val = o[key];
logic();
});
그러나 Object.keys
기본 방법이므로 더 나은 최적화가 가능할 수 있습니다.
보시다시피 Object.keys가 훨씬 빠릅니다. 실제 메모리 스토리지가 더 최적인지 여부는 다른 문제입니다.
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);
});
또한 키워드 .forEach()
로 사용할 객체를 지정 하는 두 번째 인수를 함수에 전달할 수 있습니다 this
.
// myOjbect is the object you want to iterate.
// Notice the second argument (secondArg) we passed to .forEach.
Object.keys(myObject).forEach(function(element, key, _array) {
// element is the name of the key.
// key is just a numerical value for the array
// _array is the array of all the keys
// this keyword = secondArg
this.foo;
this.bar();
}, secondArg);
node.js를 처음 사용했지만 (약 2 주) 방금 콘솔에 객체의 내용을보고하는 모듈을 만들었습니다. 모든 항목을 나열하거나 특정 항목을 검색 한 다음 필요한 경우 지정된 깊이로 드릴 다운합니다.
아마도 필요에 맞게이를 사용자 정의 할 수 있습니다. 단순하게 유지하십시오! 왜 복잡합니까? ...
'use strict';
//console.log("START: AFutils");
// Recusive console output report of an Object
// Use this as AFutils.reportObject(req, "", 1, 3); // To list all items in req object by 3 levels
// Use this as AFutils.reportObject(req, "headers", 1, 10); // To find "headers" item and then list by 10 levels
// yes, I'm OLD School! I like to see the scope start AND end!!! :-P
exports.reportObject = function(obj, key, level, deep)
{
if (!obj)
{
return;
}
var nextLevel = level + 1;
var keys, typer, prop;
if(key != "")
{ // requested field
keys = key.split(']').join('').split('[');
}
else
{ // do for all
keys = Object.keys(obj);
}
var len = keys.length;
var add = "";
for(var j = 1; j < level; j++)
{
// I would normally do {add = add.substr(0, level)} of a precreated multi-tab [add] string here, but Sublime keeps replacing with spaces, even with the ["translate_tabs_to_spaces": false] setting!!! (angry)
add += "\t";
}
for (var i = 0; i < len; i++)
{
prop = obj[keys[i]];
if(!prop)
{
// Don't show / waste of space in console window...
//console.log(add + level + ": UNDEFINED [" + keys[i] + "]");
}
else
{
typer = typeof(prop);
if(typer == "function")
{
// Don't bother showing fundtion code...
console.log(add + level + ": [" + keys[i] + "] = {" + typer + "}");
}
else
if(typer == "object")
{
console.log(add + level + ": [" + keys[i] + "] = {" + typer + "}");
if(nextLevel <= deep)
{
// drop the key search mechanism if first level item has been found...
this.reportObject(prop, "", nextLevel, deep); // Recurse into
}
}
else
{
// Basic report
console.log(add + level + ": [" + keys[i] + "] = {" + typer + "} = " + prop + ".");
}
}
}
return ;
};
//console.log("END: AFutils");
그의 코드를 조정하십시오.
Object.prototype.each = function(iterateFunc) {
var counter = 0,
keys = Object.keys(this),
currentKey,
len = keys.length;
var that = this;
var next = function() {
if (counter < len) {
currentKey = keys[counter++];
iterateFunc(currentKey, that[currentKey]);
next();
} else {
that = counter = keys = currentKey = len = next = undefined;
}
};
next();
};
({ property1: 'sdsfs', property2: 'chat' }).each(function(key, val) {
// do things
console.log(key);
});
키 / 값의 간단한 반복을 위해 때로는 밑줄 과 같은 라이브러리가 친구가 될 수 있습니다.
const _ = require('underscore');
_.each(a, function (value, key) {
// handle
});
참고로
참고 URL : https://stackoverflow.com/questions/7440001/iterate-over-object-keys-in-node-js
'Programing' 카테고리의 다른 글
파이썬에서 객체 속성을 반복 (0) | 2020.07.02 |
---|---|
텍스트 상자에 자리 표시 자 텍스트 추가 (0) | 2020.07.02 |
SQL Server 연결 문자열에서 포트 번호를 지정하는 방법은 무엇입니까? (0) | 2020.07.02 |
Gzip과 축소 (0) | 2020.07.02 |
델리게이트로 C # 옵저버 / 관측 가능한 매우 간단한 예 (0) | 2020.07.02 |