尝试使用循环向 Javascript 对象添加多个属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15907052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:32:17  来源:igfitidea点击:

Trying to add multiple properties to Javascript object using a loop

javascriptobjectloopsproperties

提问by Jonathon Nordquist

I hope the day finds you well.

我希望这一天能找到你。

So I have an object with no properties. I'm trying to add multiple properties to this object using a loop. Each property added to the loop will appear in the object multiple times depending on how many times the loop runs, with each new property incremented by 1.

所以我有一个没有属性的对象。我正在尝试使用循环向该对象添加多个属性。添加到循环中的每个属性将在对象中多次出现,具体取决于循环运行的次数,每个新属性增加 1。

So I have something like this:

所以我有这样的事情:

myObject = {  };

for(i = 0; i < 2; i++){
    myObject.propA + i = foo;
    myObject.propB + i = bar;
};

Which I want to yield something like this:

我想产生这样的东西:

myObject.propA0 = foo;
myObject.propB0 = bar;
myObject.propA1 = foo;
myObject.propB2 = bar;

Giving a nice stack of objects generated on the fly depending on how many times the loop runs. But I don't seem to be getting this. So how exactly do I feed the variable from the loop to the property when it's created and assigned?

根据循环运行的次数,提供一堆动态生成的对象。但我似乎没有得到这个。那么,在创建和分配变量时,我究竟如何将变量从循环提供给属性?

回答by TommyBs

Try using square bracket notation for the names

尝试对名称使用方括号表示法

   myObject['propa' + i] = foo;

回答by bfavaretto

As other users said, you have to use bracket notation to refer to properties by their name strings:

正如其他用户所说,您必须使用括号表示法通过名称字符串来引用属性:

myObject['propA' + i] = 'foo';

But why don't you use an array of objects, instead of a single object with similar, numbered property names? Something like this:

但是为什么不使用对象数组,而不是使用具有相似编号的属性名称的单个对象呢?像这样的东西:

var myArray = [];
for(i = 0; i < 2; i++){
    myArray.push({
        propA: 'foo',
        propB: 'bar'
    });
};

This should produce:

这应该产生:

[
    { propA: 'foo', propB: 'bar'},
    { propA: 'foo', propB: 'bar'}
]

It looks way cleaner, in my opinion.

在我看来,它看起来更干净。

回答by Chris Tonkinson

Use the array-access method to set the properties.

使用数组访问方法设置属性。

myObject = {  };

for(i = 0; i < 2; i++){
    myObject['propA' + i] = foo;
    myObject['propB' + i] = bar;
};

回答by GameAlchemist

you might use

你可能会用

object['popA'+i]=... 

to create a standard property or either use a getter/setter property, in this case you need to use

创建标准属性或使用 getter/setter 属性,在这种情况下,您需要使用

Object.defineProperty(object, *propertyname*, *propertyDescriptor*).  

The latter gives you more options on the created property.

后者为您提供了更多关于 created 属性的选项。

All details here :
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineProperty

这里的所有详细信息:https:
//developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineProperty