0

I have below jsons shown below :

     json1=
             {
    '201801': 58,
    '201802': 74,
    careerLevel: 'Analyst',
    careerLevels: [{
            '201801': 29,
            '201802': 37,
            careerID: '10000100'
        },
        {
            '201801': 29,
            '201802': 37,
            careerID: '10000110'
        }
    ]
}



     json2 = 
         {
      '201801': 58,
      '201802': 74,
      careerLevel: 'Consultant',
      careerLevels: [{
              '201801': 29,
              '201802': 37,
              careerID: '10000100'
          },
          {
              '201801': 29,
              '201802': 37,
              careerID: '10000110'
          }
      ]
  }

And in result i need to merge them dynamically so that they should look like below :

result=
  careerLevelGroups: [{
        '201801': 58,
        '201802': 74,
        careerLevel: 'Analyst',
        careerLevels: [{
                '201801': 29,
                '201802': 37,
                careerID: '10000100'
            },
            {
                '201801': 29,
                '201802': 37,
                careerID: '10000110'
            }
        ]
    },
    {
        '201801': 58,
        '201802': 74,
        careerLevel: 'Consultant',
        careerLevels: [{
                '201801': 29,
                '201802': 37,
                careerID: '10000100'
            },
            {
                '201801': 29,
                '201802': 37,
                careerID: '10000110'
            }
        ]
    }
]

Like the above there will be more jsons of same format so i think i need some loop with which i can directly attach all of them. I have tried :

jsonMap = new TSMap();
jsonMap.set("careerLevelGroups",[]);
   jsonMap.toJson().concat( json1.concat(json2)); // this is not giving me right answer

I think i need some loop as i need to add all of them dynamically.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
rp4361
  • 433
  • 1
  • 5
  • 20
  • 3
    `careerLevelGroups = [json1, Object.assign({}, json1, json2)];` – Jared Smith Mar 29 '18 at 14:49
  • 2
    or just: var careerLevelGroups = [json1, json2] – Thomas Mar 29 '18 at 15:00
  • 1
    There is no JSON in your question. So again, please read [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – str Mar 29 '18 at 15:03
  • str- i will read through the link, thanks – rp4361 Mar 29 '18 at 15:16

1 Answers1

0

You can do:

const json1 = {'201801': 58,'201802': 74,careerLevel: 'Analyst',careerLevels: [{'201801': 29,'201802': 37,careerID: '10000100'},{'201801': 29,'201802': 37,careerID: '10000110'}]};
const json2 = {'201801': 58,'201802': 74,careerLevel: 'Consultant',careerLevels: [{'201801': 29,'201802': 37,careerID: '10000100'},{'201801': 29,'201802': 37,careerID: '10000110'}]};

const result = {
  careerLevelGroups: [json1, json2]
};

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
  • thanks but can you please tell me how this magic happens because how json gets processed just by separating them by comma. – rp4361 Mar 29 '18 at 15:51
  • You have the json variables.. One way is to create an array from them: `[json1, json2]` – Yosvel Quintero Mar 29 '18 at 15:57
  • And the second example is if you have multiple variables `json1, json2, ..., json[n]` than you can iterate and use [eval()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) method – Yosvel Quintero Mar 29 '18 at 15:59