Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to merge objects in javascript without override values

how can i merge duplicate key in objects and concat values in objects in one object i have objects like this

var object1 = {
    role: "os_and_type", 
    value: "windows"
};
var object2 = {
    role: "os_and_type", 
    value: "Android"
};
var object3 = {
    role: "features", 
    value: "GSM"
};

how can i achieve this object

new_object = [{
    role: "os_and_type",
    value: ["windows", "android"]         
}, {
    role: "features",
    value: ["GSM"]
}];
like image 773
harman Avatar asked Feb 01 '14 07:02

harman


People also ask

How do I combine multiple objects into one JavaScript?

JavaScript Merge Objects To merge objects into a new one that has all properties of the merged objects, you have two options: Use a spread operator ( ... ) Use the Object. assign() method.

Does object assign overwrite?

Properties in the target object are overwritten by properties in the sources if they have the same key. Later sources' properties overwrite earlier ones. The Object. assign() method only copies enumerable and own properties from a source object to a target object.

How do you combine two objects in an array?

concat is the proper way to merge 2 arrays into one.

What is deep merge in JavaScript?

Deep merging ensures that all levels of the objects we merge into another object are copied instead of referencing the original objects. In this article, we'll look at how to deep merge JavaScript objects.


1 Answers

Here you go:

var object1 = {
    role: "os_and_type", 
    value: "windows"
};
var object2 = {
    role: "os_and_type", 
    value: "Android"
};
var object3 = {
    role: "features", 
    value: "GSM"
};

function convert_objects(){
    var output  = [];
    var temp    = [];
    for(var i = 0; i < arguments.length; i++){  // Loop through all passed arguments (Objects, in this case)
        var obj = arguments[i];                 // Save the current object to a temporary variable.
        if(obj.role && obj.value){              // If the object has a role and a value property
            if(temp.indexOf(obj.role) === -1){  // If the current object's role hasn't been seen before
                temp.push(obj.role);            // Save the index for the current role
                output.push({                   // push a new object to the output,
                    'role':obj.role,
                    'value':[obj.value]         //   but change the value from a string to a array.
                });
            }else{                              // If the current role has been seen before
                output[temp.indexOf(obj.role)].value.push(obj.value); // Save add the value to the array at the proper index
            }
        }
    }
    return output;
}

Call it like this:

convert_objects(object1, object2, object3);

You can add as many objects to the function as you'd like.

like image 196
Cerbrus Avatar answered Oct 04 '22 22:10

Cerbrus