Skip to content

Instantly share code, notes, and snippets.

@ricealexander
Last active February 18, 2019 17:55
Show Gist options
  • Save ricealexander/4fc0a9b7946868fe611e25c0ef63af41 to your computer and use it in GitHub Desktop.
Save ricealexander/4fc0a9b7946868fe611e25c0ef63af41 to your computer and use it in GitHub Desktop.
const arr1 = ["a","b","c"];
const arr2 = ["d","e","f"];
const arr3 = ["g","h","i"];
// Array.prototype.concat()
const arrayConcat = arr1.concat(arr2).concat(arr3);
// Array.prototype.push() + spread operator
const arrayPush = [];
arrayPush.push(...arr1, ...arr2, ...arr3);
// Array constructor + spread operator
const arrayConstructor = Array(...arr1, ...arr2, ...arr3);
// Array constructor + Array.prototype.flat()
const flatArrayConstructor = Array(arr1, arr2, arr3).flat();
// Array.of + spread operator
const arrayOf = Array.of(...arr1, ...arr2, ...arr3);
// Array.of + Array.prototype.flat
const flatArrayOf = Array.of(arr1, arr2, arr3).flat();
// Spread Operator
const arraySpread = [...arr1, ...arr2, ...arr3];
// Array.prototype.flat
const flatArraySpread = [arr1, arr2, arr3].flat();
console.log({
arrayConcat,
arrayPush,
arrayConstructor,
flatArrayConstructor,
arrayOf,
flatArrayOf,
arraySpread,
flatArraySpread
});
// {arrayConcat: Array(9), arrayPush: Array(9), arrayConstructor: Array(9), flatArrayConstructor: Array(9), arrayOf: Array(9), …}
// arrayConcat: (9) ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
// arrayConstructor: (9) ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
// arrayOf: (9) ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
// arrayPush: (9) ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
// arraySpread: (9) ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
// flatArrayConstructor: (9) ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
// flatArrayOf: (9) ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
// flatArraySpread: (9) ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
// proof of immutability
console.log({
arr1,
arr2,
arr3
});
// {arr1: Array(3), arr2: Array(3), arr3: Array(3)}
// arr1: (3) ["a", "b", "c"]
// arr2: (3) ["d", "e", "f"]
// arr3: (3) ["g", "h", "i"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment