In my possession are two elements a and b, both housing arrays. My goal is to combine the arrays contained in each element and create a new composite element that includes both the arrays and text nodes.
Here is my code snippet:
var a = {
name1:[1,2],
name2:[3],
name3:'alisha',
name4:'japan'
};
var b = {
name1:[4],
name2:[5,6],
name3:'hello alisha!!'
};
newobject = function (obj1, obj2) {
var obj3 = {};
for (var attrname in obj1) {
obj3[attrname] = obj1[attrname];
}
for (var attrname in obj2) {
obj3[attrname] = obj2[attrname];
}
return obj3;
};
console.log(newobject(a,b))
Purpose of the above code:
I have two objects a & b which may contain varying as well as similar data. The intention is to merge the array data if both objects share the same property. For string properties, I wish to update them with matching properties from object **b**.
The provided output may not be precise but it serves as a valuable hint towards the solution
Desired Output:
{
name1:[1,2,4],
name2:[3,5,6],
name3:'hello alisha!',
name4:'japan'
}
Please refrain from utilizing jQuery