-3

Create a javascript object with one key but multiple value I want something like this created with a for loop

'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?

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

  • 1
    By using either dynamic property names or square bracket notation for property access. – djfdev Nov 04 '19 at 22:52
  • 2
    I'm sure you'll get a useful answer if you supply a short example of the source of your data. A list that can be easily copied and pasted would work well. – SQL Hacks Nov 04 '19 at 22:52
  • Does this answer your question? [How do I create a dynamic key to be added to a JavaScript object variable](https://stackoverflow.com/questions/2462800/how-do-i-create-a-dynamic-key-to-be-added-to-a-javascript-object-variable) – Jesse Reza Khorasanee Nov 04 '19 at 23:00
  • Looks like you should do your own homework and a little research, or at least make an attempt to solve it first before coming here and getting someone else to do it for you.. I gave a downvote. – Spangle Nov 04 '19 at 23:00
  • spangle here is what I tried for(var i = 0; i < data.EndpointsResponse.Item.length; i++) { address[data.EndpointsResponse.Item[i].Address] = { 'ChannelType': data.EndpointsResponse.Item[i].ChannelType }; } Happy????? – crystal2515 Nov 04 '19 at 23:08
  • Please add that to your question above, also please give an example of what that the data looks like in your question above. – Spangle Nov 04 '19 at 23:09
  • Your second code block is not legal javascript so that's simply not valid which makes the question hard to do anything with. – jfriend00 Nov 04 '19 at 23:40

1 Answers1

-1

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...

For example, if you have a array of strings you can walk into and use the value as key of a new 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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Yeinso Blanco
  • 193
  • 1
  • 6