JS Utils Kit - v0.5.1
    Preparing search index...

    Function mergeObj

    • Deeply merges multiple source objects into one.

      • Object properties are merged recursively.

      • If a key exists in multiple objects:

        • If the value is a plain object, it is deeply merged.
        • If the value is an array:
          • If appendArray is true, arrays are concatenated.
          • If appendArray is false (default), later arrays replace earlier ones.
        • Primitive values (string, number, boolean, etc.) are overwritten by later sources.
      • Objects passed later in the list of sources have higher priority. For example, values in the last object will override previous ones.

      • If you want to give priority to a specific object, pass it last.

      Parameters

      • appendArray: boolean = false

        If true, arrays are concatenated. If false (default), arrays are replaced.

      • ...sources: object[]

        One or more objects to deeply merge.

      Returns object

      A new object containing deeply merged keys and values.

      const defaultConfig = { env: "dev", features: ["a"], flags: { debug: true } };
      const userConfig = { env: "prod", features: ["b"], flags: { beta: true } };

      const result = mergeObj(defaultConfig, userConfig);
      // {
      // env: "prod",
      // features: ["b"], // replaced, not merged
      // flags: { debug: true, beta: true }
      // }

      const result2 = mergeObj(true, defaultConfig, userConfig);
      // {
      // env: "prod",
      // features: ["a", "b"], // arrays merged
      // flags: { debug: true, beta: true }
      // }