-2

I have an array of names:

var namesArray = [Matthew, Mark, Luke, John]

And I want to convert each to a variable of the same or similar name, ie:

var MatthewVar = 1;
var MarkVar = 2;
var LukeVar = 3;
var JohnVar = 4;

But I need to do this dynamically (let's say the array has many more names in it).

This is what I had, but it doesn't like the var name:

for(var i = 0; i<namesArray.length; i++){
    var (namesArray[i] + 'Var') = namesArray[0].indexOf(namesArray[i]) + 1;
}

Thoughts?

JackNapier
  • 318
  • 4
  • 14
  • 3
    Don’t use variable names containing data (the name). ‘Problem avoided’: let people = [{name: “Luke”, worth: 3}, ..] .. transform and use as desired. Also, an object or Map (with names as keys) might be more suitable than an array. – user2864740 Jul 18 '20 at 04:12
  • If *really* wanting to go down this route (**not recommended here**), search for “variable variables” or “dynamic variables”. Numerous answers will contain the above comment recommendation/sentiments (as well as additional useful information) .. and there are are _many_ duplicates. – user2864740 Jul 18 '20 at 04:15
  • 2
    Why are you trying to do this? – user120242 Jul 18 '20 at 04:29
  • 2
    2 options: inside your for loop do `window[namesArray[i]] = i+1` or `eval(namesArray[i]+"=i+1")` then you can see and try `console.log(Matthew) ...` – Karl L Jul 18 '20 at 04:36
  • 3
    @KarlL the bigger problem here is that there is basically never a legitimate reason to want to do this, other than for brute hacks for interfacing with what is usually broken code that should be fixed at the source, or just for some toy script and you're being lazy, or some kind of monkey patching. Even for nasty monkey patch hacks there's normally still a better way. – user120242 Jul 18 '20 at 04:52
  • 3
    @user120242 I agree with you, but for the sake of satisfying this question (give the OP a solution); and I could not judge deeply whether it's a good or bad practice to do just basing on the question itself. If you overthink it, then yes there is a bigger problem but Maybe, the OP is just starting out, or just an exercise. I believe the best way to do is to provide the plausible solutions and guide the OP to the right path, not lashing out on the learners. And I believe that's the main essence of SO. – Karl L Jul 18 '20 at 05:06
  • 1
    @KarlL In this case (IMO) to require the OP to address the reason and provide context as to why they are doing this is important to the question and is legitimate to expect. (There is a small possibility they have to do this, but very unlikely. If it was necessary they'd probably know it.). It is the natural (and IMO correct) response to downvote for poor quality or close this for "needs details". Especially because there are a large amount of dupes that cover this topic. – user120242 Jul 18 '20 at 05:43
  • 2
    Which is why I see it as less a "personal" lashing out, but more of a "this question doesn't have enough context to be of value, and is potentially misleading". The only alternative response to avoid discouraging or punishing the OP, is to close it as a duplicate until the OP can provide context to legitimize the question's goal. – user120242 Jul 18 '20 at 05:57
  • @KarlL Hey guys, unfortunately I don't follow what you're suggesting. The array has to be in the format ['Matthew', 'Mark' etc] and I need it to automatically take whatever the array value is, and make it's position into a variable. So I can call the variable 'MatthewVar' or something and it would equal 1, and 'MarkVar' would equal 2. Can that be done. – JackNapier Jul 18 '20 at 06:03
  • 2
    It will help to see the code that requires accessing "MarkVar". If the answer below using an object map doesn't work you may need to provide more context. window and global variables do not behave the same in Google Apps Scripts, since they are executing in the context of Google Apps context's script engine and not browser scripting. – user120242 Jul 18 '20 at 06:10
  • @JackNapier - what the folks here are saying is that a possible valid answer to your question would be: "just don't". Note that there is littly to no "must" in programming (you can't do something that is not in the language, ofc). There are 0 valid cases where you would *want* to dynamically create variables. Variable names certainly should not be coupled with data they hold - they are called *variables* for a reason. Could we see the code? – Oleg Valter is with Ukraine Jul 18 '20 at 17:47
  • @user120242 all I am saying is. it's always better to use nicer words in our `constructive criticism`. hope you have a good day. :) – Karl L Jul 20 '20 at 01:41

1 Answers1

1
const namesArray = ['Matthew', 'Mark', 'Luke', 'John'];

const namesObject = namesArray.reduce((namesObject, name, index) => {
  namesObject[name] = index + 1;
  return namesObject;
}, {});

console.log(namesObject); // { Matthew: 1, Mark: 2, Luke: 3, John: 4 }

console.log(namesObject.Matthew); // 1

console.log(namesObject['Matthew']); // 1
GirkovArpa
  • 4,427
  • 4
  • 14
  • 43