0

I've got an object as per below:

{
id: 6,
url: 'https://google.com',
title: 'abc',
actions: 63,
status: 'active',
method: 'GET',
response_type: 'json' 
}

Now I want to push one more element in this object, the expected output is as per below:

{
id: 6,
url: 'https://google.com',
title: 'abc',
actions: 63,
status: 'active',
method: 'GET',
response_type: 'json',
new_ele: 'new_data'
}
ProgrammerPer
  • 1,125
  • 1
  • 11
  • 26
Kunal Dholiya
  • 335
  • 2
  • 6
  • 19

1 Answers1

3

If you want to do this by most simplistic way

var obj = {
id: 6,
url: 'https://google.com',
title: 'abc',
actions: 63,
status: 'active',
method: 'GET',
response_type: 'json' 
}

then use obj.new_ele = 'new data' or if your key is dynamic then you can use obj[new_ele] = 'new data'.

Otheriwse if your environment supports ES6/later version of ES* syntax

use Object.assign(obj, {new_ele: "new data"});

Even with spread operator this becomes

let newObj = { ...obj, new_ele: 'new data' };
Navoneel Talukdar
  • 4,393
  • 5
  • 21
  • 42