0

Desired output--

[
  {
    "group1": {
      "Token1": "123443423",
      "Token2": "121414141"
    },
    "group2": {
      "Token1": "123443423",
      "Token2": "121414141"
    }
  }
]

Group1 and group2 is dynamic and also the token1 and token2 value is dynamic. So i write this way --

[ {`${group1}`:{ 
         "Token1" : `${token1}`,
         "Token2" : `${token2}`
              },
        `${group2}`:{ 
         "Token1" : `${token1}`,
         "Token2" : `${token2}`
              }
 }]

But ${group1} shows error unexpected token ` (template literate).

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
owlz
  • 31
  • 1
  • 6
  • your question is not clear show your code associated with this json also describe more about what you want to achieve – Amir Rahman Sep 20 '21 at 05:36
  • i wrote the desired output above . the dynamic data in group1,group2 can be anything and those are variables coming from form input and i need to put that groups inside json array which further consists on many objects. – owlz Sep 20 '21 at 05:40
  • Wrap the template literals in `[ ]` when defining dynamic keys. (Or, you could set the properties separately rather than using an object literal.) But I don't see the point in a template literal at all, you can just write `[group1]` and `token1`, etc – CherryDT Sep 20 '21 at 06:54
  • @CherryDT your comment is the ans i was looking specifically . Thank you. – owlz Sep 20 '21 at 07:12
  • Check this link; https://stackoverflow.com/questions/33194138/template-string-as-object-property-name – SaminatorM Sep 27 '21 at 06:05

2 Answers2

1

1. [Specific]

// helper function
function createGroup(groupName, token1, token2) {
  const group = {};
  group[groupName] = {
    Token1: token1,
    Token2: token2
  };
  return group;
}

//and then create a result output
var result = [
  createGroup('group1', group1token1, group1token2),
  createGroup('group2', group2token1, group2token2),
]

2. [More general] For multiple paramenters (more than fixed 2):

your parametersObject have to be as:

{
  Token1: 'token_1_value_here',
  Token2: 'token_2_value_here',
  ...
  ParameterN: 'parameter_n_value_here',
  ...
}

And then:

// helper function 2
function createGroup(groupName, parametersObject) {
  const group = {};
  group[groupName] = parametersObject;
  return group;
}

//and then create a result output
var result = [
  createGroup('group1', group1parametersDto),
  createGroup('group2', group2parametersDto)
]
Ihar Dziamidau
  • 337
  • 2
  • 9
0

in your way you can create object with variables value this way . there was syntax error in your approach, template literals are not allowed for object key inside object directly

var group = "group",
      Token1 = "123443423",
      Token2 = "121414141"
var newObject = 
{
  [group + "1"] : {Token1 , Token2},
  [group + "2"] : {Token1 , Token2},
  [group + "3"] : {Token1 , Token2},
  [group + "4"] : {Token1 , Token2}    
}
console.log(newObject)

there is also more easy way to do same which is more conveneint example below

var object = {},
    Token1 = "123443423",
    Token2 = "121414141"

object["group1"] = {Token1,Token2}
object["group2"] = {Token1,Token2}
object["group3"] = {Token1,Token2}

console.log(object)
Amir Rahman
  • 1,076
  • 1
  • 6
  • 15