Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

¿Qué es un Array? Operaciones básicas de un Array en JavaScript

Tiempo de lectura: < 1 minuto

¡Buenos días amigos!

En el tutorial de hoy, os voy a hablar sobre los Arrays en JavaScript.

En primer lugar, ¿Qué es un array?

Un array en JavaScript es un objeto que permite almacenar una colección de valores de diferentes tipos, incluyendo otros objetos y funciones. Los arrays son muy útiles para representar conjuntos de datos de una forma más legible y para poder iterar sobre ellos mediante el uso de bucles o funciones de iteración.

Aquí hay algunos ejemplos de cómo puedes crear y utilizar arrays en JavaScript:

  • Crear un array vacío:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let array = [];
let array = [];
let array = []; 
  • Crear un array con elementos:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let array = [1, 2, 3, 4, 5];
let array = [1, 2, 3, 4, 5];
let array = [1, 2, 3, 4, 5];
  • Recorrer un array:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
// Output: 1, 2, 3, 4, 5
let array = [1, 2, 3, 4, 5]; for (let i = 0; i < array.length; i++) { console.log(array[i]); } // Output: 1, 2, 3, 4, 5
let array = [1, 2, 3, 4, 5];

for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

// Output: 1, 2, 3, 4, 5
  • Añadir elementos a un array:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let array = [1, 2, 3, 4, 5];
array.push(6);
console.log(array);
// Output: [1, 2, 3, 4, 5, 6]
let array = [1, 2, 3, 4, 5]; array.push(6); console.log(array); // Output: [1, 2, 3, 4, 5, 6]
let array = [1, 2, 3, 4, 5];

array.push(6);

console.log(array);

// Output: [1, 2, 3, 4, 5, 6]
  • Eliminar elementos de un array:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let array = [1, 2, 3, 4, 5];
array.splice(2, 1); //Borra el elemento que se encuentra en la posición 2 y un total de 1 elemento
console.log(array);
// Output: [1, 2, 4, 5]
let array = [1, 2, 3, 4, 5]; array.splice(2, 1); //Borra el elemento que se encuentra en la posición 2 y un total de 1 elemento console.log(array); // Output: [1, 2, 4, 5]
let array = [1, 2, 3, 4, 5];

array.splice(2, 1); //Borra el elemento que se encuentra en la posición 2 y un total de 1 elemento

console.log(array);

// Output: [1, 2, 4, 5]

Espero que os juste, nos vemos en el próximo.

1

Deja un comentario