IonIon
Packages
  • next
  • current
Changelog
GitHub
Packages
  • next
  • current
Changelog
GitHub
  • Version 0.x

    • Release Notes
    • Upgrade Guide
    • Contribution Guide
    • Security Policy
    • Code of Conduct
    • Origin
  • Packages

    • Introduction
    • Container

      • Introduction
      • Prerequisites
      • How to install
      • Container Instance
      • Bindings
      • Dependencies
      • Resolving
      • Contextual Bindings
    • Contracts

      • Introduction
      • How to install
    • Support

      • Introduction
      • How to install
      • Arrays

        • About Arrays
        • Includes All
        • Includes Any
        • Is Array Like
        • Is Concat Spreadable
        • Is Safe Array Like
        • Is Typed Array
        • Merge
      • Concerns

        • About Concerns
        • Prerequisites
        • Concern Class
        • Using Concerns
        • Aliases
        • Conflict Resolution
        • Booting
        • Hooks
        • Edge Cases
        • JSDoc
      • Exceptions

        • About Exceptions
        • Configure Custom Error
        • Configure Stack Trace
        • Get Error Message
        • Custom Errors
      • Facades

        • About Facades
      • Meta

        • About Meta
        • Prerequisites
        • Supported Elements
        • Set & Get
        • Inheritance
        • Outside Changes
        • TC39 Proposal
        • Target Meta
      • Mixins

        • About Mixins
        • New Mixin
        • Apply Mixins
        • Instanceof
        • Inheritance
        • Onward
      • Object

        • About Objects
        • Forget
        • Forget All
        • Get
        • Has
        • Has All
        • Has Any
        • Has Unique ID
        • Is Cloneable
        • Is Populatable
        • Isset
        • Merge
        • Populate
        • Set
        • Unique ID
      • Reflections

        • About reflections
        • Assert Has Prototype Prop.
        • Class Looks Like
        • Class Own Keys
        • Get All Parents Of Class
        • Get Class Prop. Descriptor
        • Get Class Prop. Descriptors
        • Get Constructor Name
        • Get Name Or Desc. Tag
        • Get Parent Of Class
        • Has All Methods
        • Has Method
        • Has Prototype Property
        • Is Callable
        • Is Class Constructor
        • Is Class Method Reference
        • Is Constructor
        • Is Key Safe
        • Is Key Unsafe
        • Is Method
        • Is Subclass
        • Is Subclass Or Looks Like
        • Is WeakKind
      • Misc

        • About Misc.
        • Desc. Tag
        • Empty
        • Is Key
        • Is Primitive
        • Is Property Key
        • Isset
        • Merge Keys
        • To Weak Ref.
      • Callback Wrapper
    • Vuepress Utils

      • Introduction
      • How to install
      • Navigation

        • Archive
      • Plugins

        • Last Updated
      • Components

        • Version Disclaimer
    • XYZ (test package)

merge

  • Deep Copy Objects
  • When unable to merge values
  • Merge Options Available since v0.11
    • transferFunctions
    • callback

Merges arrays into a new array. This function attempts to deep copy values, using structuredClone.

import { merge } from '@aedart/support/arrays';

const a = [ 1, 2, 3 ];
const b = [ 4, 5, 6 ];
const c = [ 7, 8, 9 ];

merge(a, b, c); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

Deep Copy Objects

Simple (or "plain") objects are deep copied. This means that new objects are returned in the resulting array.

See Mozilla's documentation for additional information about what data types are supported.

const a = { foo: 'foo' };
const b = { bar: 'bar' };
const c = { ping: 'pong' };

const result = merge([ a ], [ b, c ]);

console.log(result[0] === a); // false
console.log(result[1] === b); // false
console.log(result[2] === c); // false

When unable to merge values

In situations when values cannot be copied via structuredClone, an ArrayMergeError is thrown.

const a = [ 1, 2, 3 ];
const b = [ function() {} ]; // A function cannot be deep copied...

merge(a, b); // ArrayMergeError

See merge options for details on how to deal with functions.

Merge Options Available since v0.11

merge() supports a number of options. To specify thom, use the using() method.

merge()
    .using({ /** option: value */ })
    .of(arrayA, arrayB, arrayC);

Note

When invoking merge() without any arguments, an underlying array Merger instance is returned.

transferFunctions

By default, functions are not transferred (not copied). When encountered an ArrayMergeError is thrown, because the underlying structuredClone is not able to duplicate functions. To change this behaviour, you can set the transferFunctions setting to true. Function are then "transferred" into the resulting array.

const foo = () => true;
const bar = () => false;

merge()
    .using({ transferFunctions: true })
    .of([ foo ], [ bar ]) // [ foo, bar ]

callback

If you require more advanced duplication logic of the array values, then you can specify a callback that can process and return the value in question.

const a = [ 1, 2 ];
const b = [ 3, 4 ];

const result = merge()
    .using({
        callback: (element, index, array, options) => {
            return element * 2;
        }
    })
    .of(a, b); // [ 2, 4, 6, 8 ]

Arguments

  • element: any - The current element being processed in the array.
  • index: number - The index of the current element being processed in the array.
  • array: any[] - The concatenated array this callback was called upon.
  • options: Readonly<ArrayMergeOptions> - The merge options to be applied.
Edit page
Last Updated:
Contributors: alin
Prev
Is Typed Array