JavaScript Array concat() Method
Uniendo dos arrays:
var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale); //["Cecilie","Lone","Emil","Tobias","Linus"]
Syntax
array1.concat(array2, array3,..., arrayX)
Uniendo tres arrays:
var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var kai = ["Robin"];
var children = hege.concat(stale,kai); //["Cecilie","Lone","Emil","Tobias","Linus","Robin"]
JavaScript Array indexOf() Method
Busca el elemento Apple en el array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple"); //Resultado 2
Syntax
array.indexOf(item,start)
Busca el item "Apple"en el array, empezando desde la posición 4:
var fruits = ["Banana", "Orange", "Apple", "Mango", "Banana", "Orange", "Apple"];
var a = fruits.indexOf("Apple", 4); //Resultado 6
JavaScript Array join() Method
Une los elementos de un array dentro de un string:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.join(); //"Banana,Orange,Apple,Mango"
Syntax
array.join(separator)
Usando diferentes separadores:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.join(" and "); //"Banana and Orange and Apple and Mango"
JavaScript Array pop() Method
Borra el ultimo elemento de un array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop(); //["Banana","Orange","Apple"]
Syntax
array.pop()
JavaScript Array push() Method
Agrega un nuevo item a un array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi"); //["Banana","Orange","Apple","Mango","Kiwi"]
Syntax
array.push(item1, item2, ..., itemX)
Agrega mas de un elemento:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi", "Lemon", "Pineapple"); //["Banana","Orange","Apple","Mango","Kiwi","Lemon","Pineapple"]
JavaScript Array reverse() Method
Invierte el orden del array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.reverse(); //["Mango","Apple","Orange","Banana"]
Syntax
array.reverse()
JavaScript Array shift() Method
Remove the first item of an array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift(); //["Orange","Apple","Mango"]
Syntax
array.shift()
JavaScript Array unshift() Method
Agrega nuevos elementos al inicio del array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon","Pineapple"); //["Lemon","Pineapple","Banana","Orange","Apple","Mango"]
Syntax
array.unshift(item1,item2, ..., itemX)
JavaScript Array slice() Method
Selecciona elementos de un array:
var fruits = ["Banana", "Orange", "Lemon","Apple", "Mango"];
var citrus = fruits.slice(1, 3); //["Orange","Lemon"]
Syntax
array.slice(start,end)
JavaScript Array sort() Method
Ordena un array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); //["Apple","Banana","Mango","Orange"]
Syntax
array.sort(compareFunction)
Valores del parametro
compareFunction: Es una función opcional que define un orden de clasificación alternativo. La función debe devolver un valor negativo, cero o positivo, dependiendo de los argumentos, como:
- function(a, b){return a-b}
Cuando el método sort () compara dos valores, envía los valores a la función de comparación, y ordena los valores de acuerdo al valor (cero, negativo, positivo) devuelto.
Ordenamos los números de un array de forma ascendente:
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b}); //["1","5","10","25","40","100"]
JavaScript Array splice() Method
Agrega elementos en el array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi"); //["Banana","Orange","Lemon","Kiwi","Apple","Mango"]
Syntax
array.splice(index,howmany,item1,.....,itemX)
En la posicion 2 agregamos un nuevo elemento y removemos 1 elemento:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 1, "Lemon", "Kiwi"); //["Banana","Orange","Lemon","Kiwi","Mango"]
En la posicion 2, removemos 2 elementos:
ar fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 2); //["Banana","Orange"]
JavaScript Array toString() Method
Convierte un array en una string:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = fruits.toString(); //"Banana,Orange,Apple,Mango"
Syntax
array.toString()
No hay comentarios:
Publicar un comentario