Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Class notes Complete JavaScript™ Tips & Secrets for Profession

Rating
-
Sold
-
Pages
416
Uploaded on
08-07-2024
Written in
2023/2024

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

Show more Read less
Institution
Complete JavaScript™ Tips & Secrets For Profession
Course
Complete JavaScript™ Tips & Secrets for Profession











Whoops! We can’t load your doc right now. Try again or contact support.

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

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
leonardkinyua

Get to know the seller

Seller avatar
leonardkinyua Abraham Lincoln University, School Of Law
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
323
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions