1

I don't program in javascript much so feel free to tell me if this is a crazy idea.

I'd like to take values in an array and build arrays off of those values. For example, using the "people" array below, I want to create empty arrays "jack_test", "john_test", "mary_test", etc.

var people = ["jack","john","mary"];


 for (var i = 0; i < people.length; i++){
  //I'd like to execute code here that would create new arrays like jack_test = [], john_test= [], etc.
 }

UPDATE: poor question, sorry about that. I'm really at a beginners level with this stuff so bear with me. Let's try a little different scenario (sorry if it strays from original question too much):

Say I have an array like "people", though in reality, it'll be much longer. Then I have another array that has associated body weights, i.e.

var weights = [150,180,120]    

For each person, I'd like to take their starting weight in the array "weights" and add some constant to it to form variables (or as @Pointy points out, form property names) "jack_weight","john_weight" etc.

If I've set this up wrong in my mind and there's some more efficient method, please let me know.

4
  • 1
    Dunno why you want to do it, but you can't exactly replicate var jack_test = [], which is locally scoped, but you can do this either globally scoped via the window object or locally within any other object, like window[people[i]+'_test'] = [] and then console.log(jack_test) and the others will log the empty arrays. Commented Mar 4, 2015 at 22:54
  • What does it mean you want a "jack_test" array? Do you mean you want to declare the variable jack_test, and assign it an empty array? Commented Mar 4, 2015 at 22:56
  • 1
    ^ and why would you want to do something like that? Commented Mar 4, 2015 at 23:03
  • You haven't marked an answer accepted. Did either of these answers help you? Commented Mar 18, 2015 at 20:24

2 Answers 2

4

You cannot "construct" variable names in JavaScript*, but you can construct object property names.

var people = ["jack","john","mary"], tests = {};


 for (var i = 0; i < people.length; i++){
  //I'd would like to execute code here that would create new arrays like jack_test = [], john_test= [], etc.
  tests[people[i]] = "something";
 }

That will create properties of the "tests" object with names taken from your array. Furthermore, people[i] could be any expression, if you wanted to do something like add prefixes to the names.

* yes I know, there's eval(). edit and globals, which are object properties and thus a special case of the above example really, except with additional hazards ("special" global symbols etc).

Sign up to request clarification or add additional context in comments.

Comments

1

You can't exactly replicate var jack_test = [], which is locally scoped, but you can do this either globally scoped via the window object or locally within any other object.

var people = ["jack","john","mary"];
for (var i = 0; i < people.length; i++) {
  // assigns the property globally
  window[people[i]+'_test'] = []; 
}
console.log(jack_test); // []

This works because in the global scope (i.e. outside of any functions), variables like var x = 'whatever' are assigned to window, so these are synonymous:

var x = 'whatever';
window.x = 'whatever';

Instead of using window, you can assign properties dynamically to any object using the same method.

var myObj = {};
var myProp = 'foo';
myObj[myProp] = 'foo value';
console.log(myObj.foo); // 'foo value'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.