Fabian Peña
Fabian Peña

Reputation: 73

AngularJs - Issue with 'merge' function

Friends,

I am using 'merge' function in AngularJs in order to update a JSON from another customized:

angular.merge( original, customized );

Original:

{
"CUSTOM_MODULE_CONFIG": {
    "CUSTOMER": {
        "quickSearch": [
            { ... }
        ],
        "monitors": [
            { ... }
        ], 
        "menuNavigation": [
            { ... }
        ]
    }
}}

Customized:

{
"CUSTOM_MODULE_CONFIG": {
    "CUSTOMER": {
        "menuNavigation": [
            { ... }
        ]
    }
}}

But finally the Original JSON is updated removing "quicksearch" and "monitors" elements. In other examples (without including arrays) the merge function updates the JSON as expected, without removing any element.

How can I preserve all elements in the JSON?

Thanks!

Upvotes: 2

Views: 39

Answers (2)

Avnesh Shakya
Avnesh Shakya

Reputation: 3906

Try first parameter {}:

angular.merge({}, original, customized );

Upvotes: 0

Manu Obre
Manu Obre

Reputation: 2304

From the Angular merge Doc (https://docs.angularjs.org/api/ng/function/angular.merge)

If you want to preserve original objects, you can do so by passing an empty object as the target: var object = angular.merge({}, object1, object2).

Upvotes: 2

Related Questions