codequick-darkmode-logo
ВходРегистрация
  • javascript-advance

New Array Methods in ECMAScript 6

ECMAScript 6 (ES6), also known as ECMAScript 2015, introduced several powerful new features to JavaScript, including new array methods that make it easier to work with arrays. These new methods provide developers with more concise and efficient ways to perform common tasks on arrays.

The forEach() Method

The forEach() method allows you to iterate over the elements of an array and perform an action for each element. It takes a callback function as an argument, which is executed once for each element in the array.

const numbers = [1, 2, 3, 4, 5]; numbers.forEach(function(number) { console.log(number); });

Output:

1 2 3 4 5

The forEach() method does not return a new array, and mutating the original array inside the callback function will affect the original array.

The map() Method

The map() method creates a new array with the results of calling a provided function on every element in the original array. It returns a new array of the same length as the original array.

const numbers = [1, 2, 3, 4, 5]; const doubled = numbers.map(function(number) { return number * 2; }); console.log(doubled);

Output:

[2, 4, 6, 8, 10]

The map() method is useful when you need to transform the elements of an array into a new array without modifying the original array.

The filter() Method

The filter() method creates a new array with all elements that pass the test implemented by a provided function. It returns a new array containing only the elements for which the callback function returned true.

const numbers = [1, 2, 3, 4, 5]; const evenNumbers = numbers.filter(function(number) { return number % 2 === 0; }); console.log(evenNumbers);

Output:

[2, 4]

The filter() method is helpful when you want to extract specific elements from an array based on certain criteria.

The reduce() Method

The reduce() method applies a function against an accumulator and each element in the array to reduce it to a single value. It takes a callback function and an initial value for the accumulator as arguments.

const numbers = [1, 2, 3, 4, 5]; const sum = numbers.reduce(function(acc, number) { return acc + number; }, 0); console.log(sum);

Output:

15

The reduce() method is useful when you need to perform calculations on the elements of an array and produce a single value.

The find() Method

The find() method returns the first element in the array that satisfies the provided testing function. It returns undefined if no elements satisfy the condition.

const numbers = [1, 2, 3, 4, 5]; const evenNumber = numbers.find(function(number) { return number % 2 === 0; }); console.log(evenNumber);

Output:

2

The find() method is useful when you need to find a specific element in an array based on a condition.

The includes() Method

The includes() method determines whether an array includes a certain element, returning true or false as appropriate.

const numbers = [1, 2, 3, 4, 5]; console.log(numbers.includes(3)); // Output: true console.log(numbers.includes(6)); // Output: false

The includes() method is a concise way to check if an array contains a specific value.

These are just a few of the new array methods introduced in ECMAScript 6. They provide developers with more powerful and expressive ways to manipulate arrays in JavaScript. It is highly recommended to explore the full range of array methods available in ECMAScript 6 for more efficient and readable code.

For more information and examples, you can refer to the following resources: