[英]create a javascript key value object dynamically
Create a javascript object with one key but multiple value I want something like this created with a for loop用一个键但多个值创建一个 javascript object 我想用 for 循环创建这样的东西
'Addresses': {
'91e3e4ec8fbfc57477e05ca58679d830a2982a49a8d3fe60119ca84d640028ef': {
'ChannelType': 'APNS',
'Substitutions': {}
},
'test@test.com': {
'ChannelType': 'EMAIL',
'Substitutions': {}
},
}`
so addresses is the key, but inside the value of the key there is another key value pair.所以地址是键,但在键的值内部还有另一个键值对。 How do i create this dynamically in javascript?
我如何在 javascript 中动态创建它?
what i have tried is我试过的是
for(var i = 0; i < data.EndpointsResponse.Item.length; i++) {
address[data.EndpointsResponse.Item[i].Address] = {
'ChannelType':
data.EndpointsResponse.Item[i].ChannelType
};
but it is creating it in the format但它正在以格式创建它
'Addresses': {
'91e3e4ec8fbfc57477e05ca58679d830a2982a49a8d3fe60119ca84d640028ef':
'ChannelType': 'APNS',
'Substitutions': {}
,
'test@test.com':
'ChannelType': 'EMAIL',
'Substitutions': {}
,
}`
which is different from what i want这与我想要的不同
Even when you put less than necessary in the question, ill try to answering doing my best.即使您在问题中提出的问题没有必要,我也会尽力回答。
You should create a object dynamically passing the key in a variable...您应该创建一个 object 在变量中动态传递密钥...
For example, if you have a array of strings you can walk into and use the value as key of a new object...例如,如果您有一个字符串数组,您可以进入并使用该值作为新 object 的键...
const arrayofString= ["key1", "key2", "key3"];
let objectnew = {}
arrayofString.forEach(item => {
objectnew[item] = "value for item"
})
If you look a little bit through internet you can find many solve for this.如果你通过互联网稍微看一下,你可以找到很多解决这个问题的方法。 Like this, for example: Creating object with dynamic keys
像这样,例如: Creating object with dynamic keys
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.