0
function range(start, stop, step) {
    if (typeof stop == 'undefined') {
        // one param defined
        stop = start;
        start = 0;
    }

    if (typeof step == 'undefined') {
        step = 1;
    }

    if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
        return [];
    }

    var result = [];
    for (let i = start; step > 0 ? i < stop : i > stop; i += step) {
        result.push(i);
    }

    return result;
};

address =  process.env.KEY1
console.log(address)

for (i in range(1,50)){

}

I am trying to use process.env. to log a certain environment variable, I have 50 keys in my env file, I am trying to use process.env.KEY1, I can log it, but I want to loop 50 keys at a time output all how can i do

霍东坡
  • 1
  • 1
  • 1
    Does this answer your question? [How to loop through a plain JavaScript object with the objects as members](https://stackoverflow.com/questions/921789/how-to-loop-through-a-plain-javascript-object-with-the-objects-as-members) – DBS Sep 01 '22 at 10:49
  • Please visit the [help], take the [tour] to see what and [ask]. Do some research - [search SO for answers](https://www.google.com/search?q=javascript+loop+a+range+of+items+site%3Astackoverflow.com). If you get stuck, post a [mcve] of your attempt, noting input and expected output using the [\[<>\]](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Sep 01 '22 at 10:57

1 Answers1

1

If I understand you correctly. You can loop over an object using a for in loop and then use the value how you like for example. You can add a counter 'i' to the for in loop and check against this one to know if you have already checked whatever amount of objects you wanted to log.

const maxI = 10;
let i = 0;
for (let key in process.env) {
    if(i++ >= 10) break;
    console.log(key, process.env[key]);
}

It will then output the key and whatever value the key has into the console but only for as many fields as possible or maxI.

  • You are right, but I want to traverse some of them, there are some keys in env which are ADD KEY OKX three keys, I want to traverse a certain one such as 1-10 of ADD, 1-10 of KEY how can I do it – 霍东坡 Sep 02 '22 at 09:55
  • Ah I see, that much was not completely clear from the question. I updated the question. Of course if you want to use step you can just replace the 'i++ >= 10' with ` i += step; if(i >= 10) { break; } ` – Benny Schärer Sep 02 '22 at 10:09
  • What are you trying to accomplish with a function like this though? It is a very odd requirement. – Benny Schärer Sep 02 '22 at 10:16