0

Inside a for of loop I would like to create a different variable (ending with the iteraction number) for each DOM element so that I can use them latter:

for ( let i of document.querySelectorAll('.formClient.error') ) {
      var elem+iteractionNumber = i
}

I don't know upfront how many element I will get and I don't want to create global variables (this code snippet is to be used with a predefined limited scope and I don't want to pollute global scope).

Paulo Janeiro
  • 3,181
  • 4
  • 28
  • 46
  • 2
    Possible duplicate of [JavaScript Dynamic Variable Names](https://stackoverflow.com/questions/22727711/javascript-dynamic-variable-names) – Dan Lowe Jun 27 '17 at 17:11
  • Purpose behind this? You can create an object with the keys that you want. – Aftab Khan Jun 27 '17 at 17:11
  • Could do `window["elem"+i] = i`, but that being said, I'd be suspect of this implementation. Perhaps take a broader approach at what you're trying to achieve and we can suggest an alternative. – Tyler Roper Jun 27 '17 at 17:11
  • 1
    What is the point of this? Surely you'd be better off just using the data structure returned by `querySelectorAll` in the first place? – BenM Jun 27 '17 at 17:12
  • You can do `let length = document.querySelectorAll('.formClient.error').length;` this will return the length of found values – Christian4423 Jun 27 '17 at 17:12
  • @BenM You're 100% correct. But I know that only now after reading all the answers and comments :) Maybe that could be a good answer as an alternative for the approaches suggested below: just use the original data structure because there's no better and simpler alternative. Thank you. – Paulo Janeiro Jun 27 '17 at 17:32

1 Answers1

1

I guess you can do something like this:

window['someprefix_' + i] = i;

But this really isn't a good approach.

You can store this is an object, much better:

var hash = {};
// the loop
for ( let i of document.querySelectorAll('.formClient.error') ) {
  hash[i] = i;
}

If you are using ES6 features, you can do it even better, using Array's foreach:

let hash = {};
[].forEach.call(document.querySelectorAll('.formClient.error'), (item, index) => {
    hash[index] = item;
})
Christian Benseler
  • 7,907
  • 8
  • 40
  • 71
  • 1
    If he's just going to be storing them as `index`:`value` then an object might not even be necessary, could get by using a basic array. – Tyler Roper Jun 27 '17 at 17:15
  • 1
    @Santi yes, you are right. But the point is that I think his doubt was about creating variables dynamically, and I tried to show him how to do this using objects. What he will store for each key, is not clear (to be honest, I didn't understand why he needs this at all). – Christian Benseler Jun 27 '17 at 17:18
  • Agreed, I don't see a use-case for what OP is requesting myself. Though, that being said, you get what you ask for. – Tyler Roper Jun 27 '17 at 17:19
  • @ChristianBenseler Thank you, but I just edited my question so that it is clear that I don't want to create global variables (`window['some prefix_' + i] = i`. Nevertheless I think the only good option is to create an object and then extract each variable, right? – Paulo Janeiro Jun 27 '17 at 17:26