Home Reference Source

methods/concat/index.js

/**
 * Appends the elements of `otherArrays` into the given array.
 *
 * @param {Array} ary
 * @param {...*} otherArrays
 *
 * @return {Array} Returns the array itself.
 *
 * @example
 * concat([1, 2, 3], [4, 5, 6]); // => [1, 2, 3, 4, 5, 6]
 * concat(['a', 'b'], ['c'], ['d', 'e']) // => ['a', 'b', 'c', 'd', 'e']
 *
 * @example
 * rbjs([1, 2]).concat([3, 4]); // => [1, 2, 3, 4]
 * rbjs(['a', 'b']).concat(['c', 'd'], ['e'])  // => ['a', 'b', 'c', 'd', 'e']
 */
export default function concat(ary, ...otherArrays) {
    otherArrays.forEach(otherArray => {
        if (otherArray instanceof Array === false) {
            throw new TypeError(
                'Members of `otherArrays` must be an array instance');
        }
        ary.push(...otherArray);
    });
    return ary;
}