Jochen Schneider Jochen Schneider -4 years ago 120
jQuery Question

Update JavaScript Object

I know there are some similar questions, but non of them could not help me with my issue:

I have an Object:

var mapOptions = {
legend: {
data:[
{name: 'Europe', backgroundColor: 'RGB(228,101,41)'},
{name: 'APAC', backgroundColor: 'RGB(0,71,126)'},
{name: 'MENEAT', backgroundColor: 'RGB(145,0,73)'}
],
}
}


This object should be updated by this object:

var newOptions = {
legend: {
data: [
{name: 'Europe', backgroundColor: 'green'},
]
}
}


What should the update function be able to do:


  1. Update attributes: From
    'RGB(228,101,41)'
    to
    'green'

  2. Delete do not needed items: E.g. Only
    'Europe'
    item should remain.



For now I use the
jQuery extend/deep
function:

$.extend(true, mapOptions, newOptions);


It works partly. Only the Attributes are updated.

Can anybody help me to achieve the second point, how to delete/add items?

Or should I split it in 2 functions better?

Thank you for any help!

Answer Source

Based on your example, the solution could be a basic assignment:

mapOptions.legend.data = newOptions.legend.data
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download