0

I create a JSON object with the following code

var config = {"config": {
      "example": false,
      "example2": true,
    }
}; 

I want to conditionally add (or not) some values to the middle of the config object. I tried the following:

var additionalconfig = "";
if(additionalconfig){
    additionalConfig = {"additional1":true,
          "additional2":2};
  }

var config = {"config": {
      "example": false,
      additionalConfig,
      "example2": true,
    }
};

I got the extra config added as an extra node (list?) which is not what I want. My expected result is:

"config": {
      "example": false,
      "additional1":true,
      "additional2":2
      "example2": true,
    }

I tried to remove the node by using additionalconfig[0] but I got an error Unexpected token [. Than I tried to add it as a String

additionalConfig = '"additional1":true,
          "additional2":2';

But I also got an error. Do you know if its possible to achieve this and if yes how? I need to add the configuration in an exact position, not just merge the two objects

ssalc
  • 25
  • 6
  • Possible duplicate of [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – masterfloda Apr 26 '18 at 16:12
  • _some values to the middle of the config object._ JS objects don't follow any defined pattern for their keys. – Muhammad Usman Apr 26 '18 at 16:14
  • If you don't have to support IE11: `Object.assign(config, additionalconfig);` – masterfloda Apr 26 '18 at 16:14

3 Answers3

1

You can not append additional properties in middle of Javascript Object, A thing you can do is either you can append properties at the starting or at the end of Object.

var config = {
    "config": {
        "example": false,
        "example2": true,
    }
};

var additionalConfig = {
    "additional1": true,
    "additional2": 2
};

var config1 = config;

//post-append
config = { ...config,
    config: { ...config.config,
        ...additionalConfig
    }
}

console.log(config);

//pre-append

config1 = { ...config1,
    config: { ...additionalConfig,
        ...config1.config

    }
}

console.log(config1);
Nishant Dixit
  • 5,388
  • 5
  • 17
  • 29
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – rmlan Apr 26 '18 at 16:20
0

you can do it easily with Object.assign(....).

check the example below:

var config = {
  "config": {
    "example": false,
    "example2": true,
  }
};

var additionalconfig = true; // lets add the additional config.
if (additionalconfig) {
  additionalConfig = {
    "additional1": true,
    "additional2": 2
  };
} else {
  additionalConfig = {};
}

config.config = Object.assign(config.config, additionalConfig);

console.log(config)
Prince Hernandez
  • 3,623
  • 1
  • 10
  • 19
0
var config = {
      "example": false,
      "example2": true    
}; 

additionalConfig = {"additional1":true,
                    "additional2":2};

var newConfig = {};

for(var e in config){
    if(e === "example2"){
        for (var o in additionalConfig){
            newConfig[o] = additionalConfig[o];
        };
    }

    newConfig[e] = config[e];

}

config = Object.assign(newConfig);

{example: false, additional1: true, additional2: 2, example2: true}

Andriy
  • 99
  • 1
  • 5