Array methods
Returns a Boolean
find()
The find() method of Array instances returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
const array = [1, 3, 12, 144, 20]
const found = array.find((el) => el > 10);
console.log(found)
// expected output: {found: 12}
includes()
The includes() method of Array instances determines whether an array includes a certain value among its entries, returning true or false as appropriate.
const array = [1, 2, 3];
console.log(array.includes(2));
// Expected output: true
Without mutation
filter()
The filter() method of Array instances creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.
filter() always returns a new instance, even if it results in not filtering out any values.
const words = ["spray", "elite", "exuberant", "destruction", "present"];
const result = words.filter((word) => word.length > 6);
console.log({result});
// Expected output: Array ['exuberant', 'destruction', 'present']
map()
El método map() crea un nuevo array con los resultados de la llamada a la función indicada aplicados a cada uno de sus elementos.
const numbers =
function paresDobles(nums: number[]): number[] {
return nums.filter((num) => num % 2 === 0 ).map((num) => num * 2);
}
console.log(paresDobles([1, 2, 3, 4, 5, 6, 7, 8]));
// Expected output: [4, 8, 12, 16]
slice()
The slice() method of Array instances returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
const animals = ["ant", "bison", "camel", "duck", "elephant"];
const newAnimalsArray = animals.slice(2);
console.log(newAnimalsArray);
// Expected output: Array ['camel', 'duck', 'elephant']