Aquí, vamos a aprender acerca de las propiedades comunes más útiles de matriz y métodos en el código JavaScript con ejemplos .
propiedades y métodos de matriz en JavaScript comunes
Array.length | Devuelve la longitud de la matriz / número total de elementos de la array |
array [index] | Devuelve el nombre del artículo almacenado en la posición “índice” |
Array.push (elemento) | insertos en el extremo de la matriz |
Array.unshift (elemento) | insertos en la parte delantera de la matriz |
Array.pop () | Elimina elemento de la final de la matriz |
Array.shift) | Elimina artículo (desde el final de la matriz |
Array.indexOf (punto) | Devuelve el índice del elemento |
Array.splice (índice 1)) | Elimina elemento de la lista (índice debe ser proporcionada) |
Código:
<html>
<head>
<script>
var fruits = [
"apple",
"mango",
"banana",
"grapes",
"guava"
];
</script>
</head>
<body>
<script>
document.write("<h3>List Of Array Items</h3>")
document.write("<hr />");
document.write("<ul>");
for(var i=0;i<fruits.length;i++){
document.write("<li>"+fruits[i]+"</li>");
}
document.write("</ul>");
document.write("<hr />")
var size=fruits.length;
document.write("Size of Array : "+size+"<br />");
var first = fruits[0];
document.write("First Item of Array : "+first+"<br /><br />");
fruits.forEach(function(item,index,array){
document.write("Fruits["+index+"] = "+item+"<br />");
});
//Insert Item at last
fruits.push("orange");
document.write("<br />")
fruits.forEach(function(item,index,array){
document.write("Fruits["+index+"] = "+item+"<br />");
});
//Insert Item at Front
fruits.unshift("cherry");
document.write("<br />")
fruits.forEach(function(item,index,array){
document.write("Fruits["+index+"] = "+item+"<br />");
});
//Remove Item from Last
fruits.pop();
document.write("<br />")
fruits.forEach(function(item,index,array){
document.write("Fruits["+index+"] = "+item+"<br />");
});
//Remove Item from Front
fruits.shift();
document.write("<br />")
fruits.forEach(function(item,index,array){
document.write("Fruits["+index+"] = "+item+"<br />");
});
//Finding Index of Item
var index = fruits.indexOf("banana");
document.write("<br />");
document.write("Index of banana : "+index+"<br />");
//Removing any item from List
document.write("<br />");
var pos = fruits.indexOf("banana");
fruits.splice(pos,1)
fruits.forEach(function(item,index,array){
document.write("Fruits["+index+"] = "+item+"<br />");
});
</script>
</body>
</html>
salida
List Of Array Items
apple
mango
banana
grapes
guava
Size of Array : 5
First Item of Array : apple
Fruits[0] = apple
Fruits[1] = mango
Fruits[2] = banana
Fruits[3] = grapes
Fruits[4] = guava
Fruits[0] = apple
Fruits[1] = mango
Fruits[2] = banana
Fruits[3] = grapes
Fruits[4] = guava
Fruits[5] = orange
Fruits[0] = cherry
Fruits[1] = apple
Fruits[2] = mango
Fruits[3] = banana
Fruits[4] = grapes
Fruits[5] = guava
Fruits[6] = orange
Fruits[0] = cherry
Fruits[1] = apple
Fruits[2] = mango
Fruits[3] = banana
Fruits[4] = grapes
Fruits[5] = guava
Fruits[0] = apple
Fruits[1] = mango
Fruits[2] = banana
Fruits[3] = grapes
Fruits[4] = guava
Index of banana : 2
Fruits[0] = apple
Fruits[1] = mango
Fruits[2] = grapes
Fruits[3] = guava