0

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.

  • `Object.assign` merges the properties of all items into the first one supplied. You pass `{}` first, so that empty object is being updated. While that is a useful technique when you want to make a new object out of several, it is not what you want. Just remove that `{}` and you should merge properly. – Scott Sauyet Feb 23 '20 at 19:27
  • Two other things: First, by a strong convention, class names should be capitalized, so "`Layer`" instead of "`layer`". But more interestingly, you're not using the `layer` class here at all. Do you want to use it by replacing `var layer = {};` with `var layer = new layer('some name');`? – Scott Sauyet Feb 23 '20 at 19:30
  • Hi scott using just ```Object.assign(layers,fetched);``` creates the same fault.||| Also the class is called "Layer", but the object it expands is called "layers" They are seperately defined so "layers" can be expanded with the "Layer" class several times in different places. – Michiel Celis Feb 23 '20 at 20:12
  • Could it be that somehow the parts of an object expanded by a class are not accessible by Object.assign(); ? – Michiel Celis Feb 23 '20 at 20:42
  • `Object.assign` only looks at top-level properties. It just writes those of the second & third argument into the first argument object. If they happen to be the same (like `header` in your case), the right most version *overrules* the other occurrence. There is no deeper merge operation happening in that case -- by design (it's called `assign` for a reason). See linked Q&A. – trincot Feb 23 '20 at 20:47
  • Hi Michiel, please don't use that kind of code formatting, it makes your code terrible hard to read. There was a time, like 30 years ago, when we thought that this 'tabular' style was good, but we were plain wrong :) – Andreas Dolk Feb 23 '20 at 20:53
  • Hi Andreas_D, i will format my next posts to the standard. Even though my OCD disagrees, and will probably push me to make a VScode plugin that does this autom. once i'm a better programmer. – Michiel Celis Feb 23 '20 at 21:03

0 Answers0