i'm trying to merge two objects "layers" and "fetched".
- "layers" is initiated empty and then expanded by a "Layer" class.
- "fetched" is written out in this example.
Goal: The original object "layers" is to be overwritten only in its overlapping properties, and expanded by the second object "fetched".
Actual outcome:
mergedObject = Object.assign({},layers,fetched)
"fetched" is written into "mergedObject", disregarding "layers"
Object.assign(layers,fetched)
"fetched" is written into "layers", disregarding "layers"
class Layer {
constructor(layerName) {
this.properties = {
layername : layerName,
element : {
type : ''
}
}
}
}
// Objects made, that will contain functions, and placeholders for vars
var layers = {};
layers.header = new Layer('header');
layers.footer = new Layer('footer');
// Each layers Data i will ajax get from JSON files
var fetched = {};
fetched.header = {
"properties" : {
"element" : {
"type" : "bar",
"subtype" : "full"
},
"placement" : {
"to" : "top",
"z" : "0"
},
"visibility" : {
"status" : false
}
}
}
fetched.footer={
"properties" : {
"element" : {
"type" : "bar",
"subtype" : "half"
},
"placement" : {
"to" : "bottom",
"z" : "0"
},
"visibility" : {
"status" : true
}
}
}
// And now i'd like to place the fetched data into the Class created Objects with functions
mergedObject = Object.assign({},layers,fetched)
// But Object.assign( is not working ? and completely forgets the original object's unique properties
console.log('layers');
console.log(layers);
console.log('fetched')
console.log(fetched);
console.log('mergedObject');
console.log(mergedObject);
Any help is welcome.