Escrito por estudiantes que aprobaron Inmediatamente disponible después del pago Leer en línea o como PDF ¿Documento equivocado? Cámbialo gratis 4,6 TrustPilot
logo-home
Document preview thumbnail
Vista previa 4 fuera de 416 páginas
Notas de lectura

Class notes Complete JavaScript™ Tips & Secrets for Profession

Document preview thumbnail
Vista previa 4 fuera de 416 páginas

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


Información del documento

Subido en
8 de julio de 2024
Número de páginas
416
Escrito en
2023/2024
Tipo
Notas de lectura
Profesor(es)
Leonard
Contiene
Todas las clases
$8.49

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Vendido
0
Seguidores
0
Artículos
322
Última venta
-


Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes

Ups! No podemos cargar tu documento ahora. Inténtalo de nuevo o contacta con soporte.