1

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!

7
  • Update attributes: From 'RGB(228,101,41)' to 'green' not all RGB values have english names. What do you want to do in that case? Commented Feb 28, 2017 at 10:43
  • @MehulMohan Pointed the first problem. Second problem, based on what do you want to remove an item ? You only want to keep 'Europe' ? What if there is multiple times 'Europe' ? Commented Feb 28, 2017 at 10:44
  • 1.Problem: Just an example. I will use RGBs instead:) Commented Feb 28, 2017 at 10:47
  • You can just set the new object in place of the old: mapOptions.legend.data = newOptions.legend.data Commented Feb 28, 2017 at 10:48
  • 2.Problem: Yes I want just keep newOptions.data. Other Items should be deleted Commented Feb 28, 2017 at 10:51

4 Answers 4

1

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

mapOptions.legend.data = newOptions.legend.data
Sign up to request clarification or add additional context in comments.

1 Comment

Problem is that there are more than data attributes in this object. It seems like that every of them must be set witha basic assigment. Thanks for help
1

If you want just change the first valeus You could use the relative index position (0) inside the data array

 mapOptions.legend.data[0] =   {name: 'Europe', backgroundColor: 'green'};

if the you want change all the data contente then you can use the javascript dot notation for accessing object element

mapOptions.legend.data =   {name: 'Europe', backgroundColor: 'green'};

1 Comment

@RoryMcCrossan thank answer extended for full change
0

Try this

mapOptions.legend.data =   {name: 'Europe', backgroundColor: 'green'};

Comments

0

For more than one item to update, you could use a hash table with a reference to the objects and iterate then the update array.

If hash is set for a given name, update, otherwise push the new object to the array.

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)' }], } },
    newOptions = { legend: { data: [{ name: 'Europe', backgroundColor: 'green' }, ] } },
    hash = Object.create(null);

mapOptions.legend.data.forEach(function (a, i) {
    hash[a.name] = a;
});

newOptions.legend.data.forEach(function (a) {
    if (hash[a.name]) {
        hash[a.name].backgroundColor = a.backgroundColor;
    } else {
        mapOptions.legend.data.push(a);
    }
});

console.log(mapOptions.legend.data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.