Skip to content

Instantly share code, notes, and snippets.

@teffcode
Created September 26, 2017 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teffcode/1f26c3b754422031818eb7ee3caffc4e to your computer and use it in GitHub Desktop.
Save teffcode/1f26c3b754422031818eb7ee3caffc4e to your computer and use it in GitHub Desktop.
Array_Methods
/*
* FROM -> crea una nueva instancia de array
*/
// 1
Array.from("tefa") // ["t", "e", "f", "a"]
// 2
function formArrayLike() {
return Array.from(arguments)
}
formArrayLike(1, 2, 3) // [1, 2, 3]
/*
* IS ARRAY -> determina si el valor es un array
*/
// 1
Array.isArray([1,2]) // true
Array.isArray(Array.prototype) // true
Array.isArray({tefa: 2}) // false
/*
* ARRAY OF -> crea una nueva instancia de array
*/
Array.of("tefa") // ["tefa"]
/*
* CONCAT -> unir dos o más arreglos
*/
// 1
var a = [1, 2, 3]
var b = [4, 5, 6]
var ab = a.concat(b) // ab = [1, 2, 3, 4, 5, 6]
/*
* COPY WITHIN -> hace una copia de los elementos del arr
*/
// el (-2) es de atrás para adelante y desde ahí empieza a copiar
[1, 2, 3, 4, 5].copyWithin(-2) // [1, 2, 3, 1, 2]
// (-3) -> desde donde empieza a copiar
// (1) -> desde que posición hace la copia
// (-2) -> hasta donde copia
[1, 2, 3, 4, 5].copyWithin(-3, 1, -2) // [1, 2, 2, 3, 5]
/*
* EVERY -> retorna booleano al probar una función por cada elemento del array
*/
[1, 2, 3].every((c) => c<5) // true
/*
* FILL -> rellena los elementos de un array
*/
Array(4).fill(5) // [5, 5, 5, 5]
[1, 2, 3, 4].fill(3, 1, 3) // [1, 3, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment