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

5
  • your question is not clear show your code associated with this json also describe more about what you want to achieve Commented Sep 20, 2021 at 5: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. Commented Sep 20, 2021 at 5: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 Commented Sep 20, 2021 at 6:54
  • @CherryDT your comment is the ans i was looking specifically . Thank you. Commented Sep 20, 2021 at 7:12
  • Check this link; stackoverflow.com/questions/33194138/… Commented Sep 27, 2021 at 6:05

2 Answers 2

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)
]
Sign up to request clarification or add additional context in comments.

3 Comments

it works. However if i have to pass 40 variables then i should to pass those 40 parameter into helper function?
Please, share the parameters object DTO example. I will update the answer
Updated. for more general case
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.