1

Say there is a vars.js: let a = "a" let b = "bbb" let cca = "vsa"

Say there are 1000 of these variables. Is there a way to loop through them using only JS or node.js ?

Thank you in advance.

I was trying to push them all to a array so that I can loop through them.

Sing4
  • 11
  • 2
  • 1
    Why don't you just create the array/object straight away instead of defining thousands of variables? This can lead to memory issues if you define many variables. – Arnav Thorat Mar 25 '22 at 06:24
  • The question is why would you even need to define so many variables? Use either an object or an array. – Molda Mar 25 '22 at 06:36
  • Yes, use an object instead. This is _not_ what variables are for. – Sebastian Simon Mar 25 '22 at 06:43
  • So if you are using a JavaScript framework like React.js it's possible using module. so in your first file you will have something like this, let's call it `variables.js` ```js export let a = "a", b = "bbb", cca = "vsa"; ``` and in another file where you wish to spread them you can import them all like this, and get all their values in an array. ```js import * as all from "src/variables"; const myArr = all; console.log({ myArr: Object.values(myArr) }); ``` – Ridwan Ajibola Mar 25 '22 at 06:48
  • _"I was trying to push them all to a array so that I can loop through them."_ But to do that you would have to loop over them first. – Andy Mar 25 '22 at 06:53

0 Answers0