C
C
cat_crash2012-11-28 00:14:11
Node.js
cat_crash, 2012-11-28 00:14:11

NodeJS dynamically adding to array

Colleagues, tell me how to do the following more elegantly:

The code below is absolutely not working even in theory, but it clearly explains what I want:
Conditions:
some_hash - must be an object (NOT an array)
The code must turn into an “endless loop”

var some_hash={'1','2','3'}

for(var key in some_hash){
  some_hash.push(key+1); 
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
mark_ablov, 2012-11-28
@cat_crash

var names = Object.getOwnPropertyNames(obj);
for (var i = 0, len = names.length; i < len; ++i)
{
    obj['prop' + i] = i;
    names = Object.getOwnPropertyNames(obj);
    len = names.length;
}

S
Silver_Clash, 2012-11-28
@Silver_Clash

Use recursion.
I just don't understand why you need this? Do you want all Habr users to get node.js or browsers freeze?
For the sake of laughter, I wrote this in the FF console - as it should be, he ate one core.

M
Mithgol, 2012-11-28
@Mithgol

In my opinion, if you like, an infinite loop looks most elegant in the form "while (true) ...".
So the above code would look like this:

var some_hash = {};
var key = 1;
while (true) {
   some_hash.push(key++);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question