-1

I have two objects as below :

    obj1= 
{ 
      '201609': 52,
      '201610': 54,
      '201611': 56,
       metric: 'promotionsOut',
       careerLevelGroups:
       [ { '201609': 52,
           '201610': 54,
           '201611': 56,
           careerLevelGroup: 'Associate' 
          } 
        ] 
    }

        obj2= 
{'careerLevels': [{
            '201609': 21,
            '201610': 22,
            '201611': 23,
            'careerID': 10000120
        },
        {
            '201609': 31,
            '201610': 32,
            '201611': 33,
            'careerID': 10000130
        }
    ]
}

Now i need to insert obj2 in a way that the result should be

result = 
{
        "201609": 52,
        "201610": 54,
        "201611": 56,
        "metric": "PromotionsOut",
        "careerLevelGroups": [{
            "201609": 52,
            "201610": 52,
            "201611": 56,
            "careerLevelGroup": "Associate",
            "careerLevels": [{
                    "201609": 21,
                    "201610": 22,
                    "201611": 23,
                    "careerID": 10000120
                },
                {
                    "201609": 31,
                    "201610": 32,
                    "201611": 33,
                    "careerID": 10000130
                }
            ]
        }]

    }

I am trying to work on it using push method like:

let onlyCLs = obj2;
 metric_clg_json.careerLevelGroups[0].careerLevel.push(onlyCLs);

but this is not working for me. may be i need some loop logic to get to the "careerLevels" node and then insert obj2 just below it.

str
  • 42,689
  • 17
  • 109
  • 127
rp4361
  • 433
  • 1
  • 5
  • 20
  • 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 12:33

3 Answers3

2

You shouldn't be using push. push is used to insert items into an array. You want to assign an array to a property.

You should be assigning obj2.careerLevels to the careerLevels property of obj1

obj1 = {
  '201609': 52,
  '201610': 54,
  '201611': 56,
  metric: 'promotionsOut',
  careerLevelGroups: [{
    '201609': 52,
    '201610': 54,
    '201611': 56,
    careerLevelGroup: 'Associate'
  }]
}

obj2 = {
  'careerLevels': [{
      '201609': 21,
      '201610': 22,
      '201611': 23,
      'careerID': 10000120
    },
    {
      '201609': 31,
      '201610': 32,
      '201611': 33,
      'careerID': 10000130
    }
  ]
}

obj1.careerLevelGroups[0].careerLevels = obj2.careerLevels;

console.log(obj1);
Turnip
  • 35,836
  • 15
  • 89
  • 111
1

You just need to add obj2 in the array careerLevelGroups of obj1 like below:

var obj1= 
{ 
  '201609': 52,
  '201610': 54,
  '201611': 56,
  'metric': 'promotionsOut',
  'careerLevelGroups':
   [ { '201609': 52,
       '201610': 54,
       '201611': 56,
       'careerLevelGroup': 'Associate' 
      } 
    ] 
}

var obj2 =  {
    'careerLevels': [{
          '201609': 21,
          '201610': 22,
          '201611': 23,
          'careerID': 10000120
      },
      {
          '201609': 31,
          '201610': 32,
          '201611': 33,
          'careerID': 10000130
      }
  ]
}

obj1.careerLevelGroups[0]['careerLevels'] = obj2['careerLevels'];
console.log(obj1);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • i dont know why someone downvoted my question rather than trying to answer, can you please upvote. – rp4361 Mar 29 '18 at 15:53
1

obj1.careerLevelGroups[0].careerLevels = obj2.careerLevels

obj1= { 
  '201609': 52,
  '201610': 54,
  '201611': 56,
   metric: 'promotionsOut',
   careerLevelGroups:
   [ { '201609': 52,
       '201610': 54,
       '201611': 56,
       careerLevelGroup: 'Associate' 
      } 
    ] 
}

obj2={'careerLevels': [{
            '201609': 21,
            '201610': 22,
            '201611': 23,
            'careerID': 10000120
        },
        {
            '201609': 31,
            '201610': 32,
            '201611': 33,
            'careerID': 10000130
        }
    ]
}
obj1.careerLevelGroups[0].careerLevels = obj2.careerLevels
console.log(obj1)