Class notes Complete JavaScript™ Tips & Secrets for Profession
Sometimes, you need to work with an array while ensuring you don't modify the original. Instead of a clone method, arrays have a slice method that lets you perform a shallow copy of any part of an array. Keep in mind that this only clones the first level. This works well with primitive types, like numbers and strings, but not objects. To shallow-clone an array (i.e. have a new array instance but with the same elements), you can use the following one-liner: var clone = arrayToC(); This calls the built-in JavaScript A method. If you pass arguments to slice, you can get more complicated behaviors that create shallow clones of only part of an array, but for our purposes just calling slice() will create a shallow copy of the entire array. All method used to convert array like objects to array are applicable to clone an array: Version≥6 arrayToClone = [1, 2, 3, 4, 5]; clone1 = A(arrayToClone); clone2 = A(...arrayToClone); clone3 = [...arrayToClone] // the shortest way Version≤5.1 arrayToClone = [1, 2, 3, 4, 5]; clone1 = A(arrayToClone); clone2 = [].(arrayToClone); Section 3.12: Concatenating Arrays Two Arrays var array1 = [1, 2]; var array2 = [3, 4, 5]; Version≥3 var array3 = t(array2); // returns a new array Version≥6 var array3 = [...array1, ...array2] Results in a new Array: [1, 2, 3, 4, 5] Multiple Arrays var array1 = ["a", "b"], array2 = ["c", "d"], array3 = ["e", "f"], array4 = ["g", "h"]; Version≥3 Provide more Array arguments to t() Complete JavaScript™ Tips & Secrets for Professionals 45 var arrConc = t(array2, array3, array4); Version≥6 Provide more arguments to [] var arrConc = [...array1, ...array2, ...array3, ...array4] Results in a new Array: ["a", "b", "c", "d", "e", "f", "g", "h"] Without Copying the First Array var longArray = [1, 2, 3, 4, 5, 6, 7, 8], shortArray = [9, 10]; Version≥3 Provide the elements of shortArray as parameters to push using F longA(longArray, shortArray); Version≥6 Use the spread operator to pass the elements of shortArray as separate arguments to push longA(...shortArray) The value of longArray is now: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Note that if the second array is too long (100,000 entries), you may get a stack overflow error (because of how apply works). To be safe, you can iterate instead: shortAEach(function (elem) { longA(elem); }); Array and non-array values var array = ["a", "b"]; Version≥3 var arrConc = t("c", "d"); Version≥6 var arrConc = [...array, "c", "d"] Results in a new Array: ["a", "b", "c", "d"] You can also mix arrays with non-arrays var arr1 = ["a","b"]; var arr2 = ["e", "f"]; var arrConc = t("c", "d", arr2); Results in a new Array: ["a", "b", "c", "d", "e", "f
Written for
- Institution
- Complete JavaScript™ Tips & Secrets for Profession
- Course
- Complete JavaScript™ Tips & Secrets for Profession
Document information
- Uploaded on
- July 8, 2024
- Number of pages
- 416
- Written in
- 2023/2024
- Type
- Class notes
- Professor(s)
- Leonard
- Contains
- All classes
Subjects
-
complete javascript tips secrets for profession