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
  • 51,408
  • 11
  • 123
  • 146
rp4361
  • 423
  • 1
  • 5
  • 17
  • 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 Arguelles
  • 17,270
  • 4
  • 37
  • 42