简体   繁体   English

将数组值替换为循环以创建新数组

[英]Substitute array values into a loop to create new arrays

I don't program in javascript much so feel free to tell me if this is a crazy idea. 我不太用JavaScript编程,所以请随时告诉我这是否是一个疯狂的主意。

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. 例如,使用下面的“ people”数组,我想创建空数组“ jack_test”,“ john_test”,“ mary_test”等。

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, ie 然后我有另一个数组具有关联的体重,即

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. 对于每个人,我想在数组“ weights”中获取其初始权重,并向其添加一些常量以形成变量(或@Pointy指出,形成属性名称)“ jack_weight”,“ john_weight”等。

If I've set this up wrong in my mind and there's some more efficient method, please let me know. 如果我记错了,并且有更有效的方法,请告诉我。

You cannot "construct" variable names in JavaScript * , but you can construct object property names. 您不能在JavaScript *中 “构造”变量名称,但是可以构造对象属性名称。

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. 这将使用从数组中获取的名称来创建“ tests”对象的属性。 Furthermore, people[i] could be any expression, if you wanted to do something like add prefixes to the names. 此外,如果要执行诸如在名称中添加前缀的操作, people[i]可以是任何表达式。

* yes I know, there's eval() . *是的,我知道,有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). edit和globals,它们是对象的属性,因此是上述示例的一种特殊情况,除了附加的危险(“特殊”全局符号等)外。

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 jack_test = [] ,它是局部范围的,但是您可以通过window对象在全局范围内或在任何其他对象内部进行局部范围的复制。

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 (ie outside of any functions), variables like var x = 'whatever' are assigned to window, so these are synonymous: 之所以可行,是因为在全局范围内(即,在任何函数之外),将诸如var x = 'whatever'类的变量分配给window,因此它们是同义词:

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

Instead of using window , you can assign properties dynamically to any object using the same method. 除了使用window ,还可以使用相同的方法将属性动态分配给任何对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM