Logo pequeno-gafanhoto

Pequeno Gafanhoto

Array and loops

Array

Array is a built-in data structure that allows you to store and organize multiple values of any type in a single variable. It is a container that can hold a fixed number of items, and each item is referred to as an element. The elements in an array are ordered and can be accessed by their index, which is a numerical value starting from 0 for the first element.

let fruits = ["apple", "banana", "orange"];

In this example, fruits is an array that contains three elements: "apple", "banana", and "orange".

Arrays in JavaScript are dynamic, meaning you can add or remove elements from them at any time. You can access elements in an array using their index. For example, to access the first element of the fruits array, you would use fruits[0], which would give you the value "apple".

Loop

In JavaScript, you can use various types of loops to iterate over the elements of an array.

for loop

The for loop is a general-purpose loop that allows you to execute a block of code a certain number of times. You can use it to iterate over an array by utilizing the array's length and accessing elements using their indices. Here's an example:

let fruits = ["apple", "banana", "orange"];

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

for...of loop

The for...of loop is a more concise loop introduced in ES6 (ECMAScript 2015). It provides a simpler syntax for iterating over iterable objects, including arrays. With the for...of loop, you don't need to deal with indices. Here's an example:

let fruits = ['apple', 'banana', 'orange'];

for (let fruit of fruits) {
  console.log(fruit);
}

This loop will iterate over each element in the fruits array and print its value. The fruit variable takes the value of each element in each iteration.

For...await loop

The for...await loop is used for asynchronous iteration, especially when dealing with promises or async/await functions. It allows you to iterate over an array of promises or other async iterable objects. Here's an example:

async function asyncOperation(item) {
  // Perform asynchronous operation
}

let items = [1, 2, 3];

for await (let item of items) {
  await asyncOperation(item);
}

In this example, the for...await loop iterates over an array of items and performs an asynchronous operation on each item.

for...in

The for...in loop is used to iterate over the properties of an object. While it can technically be used with arrays, it is generally recommended to use other loop structures for arrays. The for...in loopiterates over the enumerable properties of an object, which includes array indices and additional properties added to the array object. Here's an example:

let fruits = ['apple', 'banana', 'orange'];

for (let index in fruits) {
  console.log(fruits[index]);
}

while

The while loop allows you to repeatedly execute a block of code as long as a specified condition is true. You can use it to iterate over an array by combining it with a variable that keeps track of the current index. Here's an example:

let fruits = ['apple', 'banana', 'orange'];
let i = 0;

while (i < fruits.length) {
  console.log(fruits[i]);
  i++;
}

do...while

The do...while loop is similar to the while loop, but it guarantees that the block of code is executed at least once before checking the condition. Here's an example:

let fruits = ['apple', 'banana', 'orange'];
let i = 0;

do {
  console.log(fruits[i]);
  i++;
} while (i < fruits.length);

This do...while loop will iterate over the fruits array and print each element's value.

forEach

Arrays in JavaScript have a built-in forEach method, which allows you to iterate over the elements of an array and execute a callback function for each element. The forEach method provides a simpler and more expressive way to loop over arrays. Here's an example:

let fruits = ['apple', 'banana', 'orange'];

fruits.forEach(function(fruit) {
  console.log(fruit);
});

The forEach method takes a callback function as an argument, which is executed for each element in the array. The fruit parameter in the example represents the current element in each iteration.

map

The map method is a higher-order function available for arrays in JavaScript. It allows you to iterate over an array and create a new array based on the transformation of each element. The map method applies a provided callback function to each element and returns a new array with the results. Here's an example:

let numbers = [1, 2, 3];

let doubledNumbers = numbers.map(function(number) {
  return number * 2;
});

console.log(doubledNumbers);  // Output: [2, 4, 6]

In this example, the map method is used to create a new array(doubledNumbers) where each element is multiplied by 2.

filter

The filter method is another higher-order function for arrays. It allows you to iterate over an array and create a new array containing only the elements that pass a certain condition specified by a callback function. Here's an example:

let numbers = [1, 2, 3, 4, 5];

let evenNumbers = numbers.filter(function(number) {
  return number % 2 === 0;
});

console.log(evenNumbers);  // Output: [2, 4]

In this example, the filter method is used to create a new array(evenNumbers) that contains only the even numbers from the original array.

find

The find method is an array method in JavaScript that returns the first element in the array that satisfies a given condition specified by a callback function. It stops the iteration once the condition is met and returns the found element. If no element satisfies the condition, it returns undefined. Here's an example usage of the find method:

let numbers = [1, 2, 3, 4, 5];

let foundNumber = numbers.find(function(number) {
  return number > 3;
});

console.log(foundNumber);  // Output: 4

In this example, the find method searches for the first number greater than 3 in the array and returns it. The callback function checks the condition number > 3 for each element until it finds a match.

reduce

The reduce method is a powerful higher-order function that allows you to iterate over an array and accumulate a single value based on the elements of the array. It applies a provided callback function to each element, taking the previously accumulated value and the current element as arguments. Here's an example:

let numbers = [1, 2, 3, 4, 5];

let sum = numbers.reduce(function(accumulator, number) {
  return accumulator + number;
}, 0);

console.log(sum);  // Output: 15

In this example, the reduce method is used to calculate the sum of all the numbers in the array.

some

The some method is an array method that tests whether at least one element in the array satisfies a given condition specified by a callback function. It returns true if any element passes the condition; otherwise, it returns false. Here's an example:

let numbers = [1, 2, 3, 4, 5];

let hasEvenNumber = numbers.some(function(number) {
  return number % 2 === 0;
});

console.log(hasEvenNumber);  // Output: true

In this example, the some method checks if there is at least one even number in the array.

every

The every method is similar to the some method, but it tests whether all elements in the array satisfy a given condition specified by a callback function. It returns true if all elements pass the condition; otherwise, it returns false. Here's an example:

let numbers = [2, 4, 6, 8, 10];

let allEvenNumbers = numbers.every(function(number) {
  return number % 2 === 0;
});

console.log(allEvenNumbers);  // Output: true

In this example, the every method checks if all numbers in the array are even.