Methods for working with arrays. Javascript example open

Methods for working with arrays. Javascript example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: ES6

Arrays in JavaScript are one of the most important data structures used in the language. They allow you to store and organize data as an ordered set of elements, each of which can contain any value, including other arrays and objects.

slice() – creates a new array containing the selected elements from the original array:

let fruits = ['apple', 'banana', 'orange', 'grape', 'kiwi'];
let slicedFruits = fruits.slice(1, 4);
console.log(slicedFruits); // ['banana', 'orange', 'grape']

splice() – modifies the contents of an array by removing, replacing or adding elements:

let fruits = ['apple', 'banana', 'orange', 'grape'];
fruits.splice(2, 1, 'kiwi', 'melon');
console.log(fruits); // ['apple', 'banana', 'kiwi', 'melon', 'grape']

//2 – the element we want to start with
//1 – how many items we want to remove

join() – joins all array elements into a string:

let fruits = ['apple', 'banana', 'orange'];
let joinedFruits = fruits.join(', ');
console.log(joinedFruits); // 'apple, banana, orange'

indexOf() – returns the index of the first element found in the array:

let fruits = ['apple', 'banana', 'orange'];
let index = fruits.indexOf('banana');
console.log(index); // 1, array starting from 0

lastIndexOf() – returns the index of the last element found in the array:

let fruits = ['apple', 'banana', 'orange', 'banana'];
let lastIndex = fruits.lastIndexOf('banana');
console.log(lastIndex); // 3

reverse() – reverses the order of elements in an array:

let fruits = ['apple', 'banana', 'orange'];
fruits.reverse();
console.log(fruits); // ['orange', 'banana', 'apple']

sort() – sorts array elements in lexicographic order:

let fruits = ['banana', 'orange', 'apple'];
fruits.sort();
console.log(fruits); // ['apple', 'banana', 'orange']

filter() – creates a new array with elements that have passed the test by the specified function:

let numbers = [1, 2, 3, 4, 5];
let evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // [2, 4]

map() – creates a new array containing the result of calling the given function for each element:

let numbers = [1, 2, 3, 4, 5];
let squaredNumbers = numbers.map(num => num ** 2);
console.log(squaredNumbers); // [1, 4, 9, 16, 25]

forEach() – executes a given function for each array element:

let fruits = ['apple', 'banana', 'orange'];
fruits.forEach(fruit => console.log(fruit));
// 'apple'
// 'banana'
// 'orange'
forEach() method performs the given function for each element of the array, but does not create a new array, unlike the map() method. It simply performs the specified function for each element without changing the original array and without returning a new array. forEach() is useful when you need to perform operations on each element of an array, but you don’t need to create a new array with the results.

includes() – checks whether the array contains the specified element, returning true or false:

let fruits = ['apple', 'banana', 'orange'];
console.log(fruits.includes('banana')); // true
console.log(fruits.includes('grape')); // false

every() – checks whether all elements of the array satisfy a given condition, returning true or false:

let numbers = [2, 4, 6, 8, 10];
let allEven = numbers.every(num => num % 2 === 0);
console.log(allEven); // true

some() – checks whether at least one array element satisfies a given condition, returning true or false:

let numbers = [1, 3, 5, 7, 10];
let hasEven = numbers.some(num => num % 2 === 0);
console.log(hasEven); // true

find() – returns the first element found in the array that satisfies the given condition:

let numbers = [1, 2, 3, 4, 5];
let evenNumber = numbers.find(num => num % 2 === 0);
console.log(evenNumber); // 2

The reduce() method in JavaScript applies an accumulator function to each element of the array, returning a single resulting value. It allows you to collapse (reduce) an array to a single value by applying a function to each element sequentially.

array.reduce(function(accumulator, currentValue, index, array) {
  // Array element processing logic
}, initialValue);
  • accumulator : an accumulator, a value that will be passed from iteration to iteration and eventually become the resulting value.
  • currentValue : the current array element being processed
  • index (optional): index of the current array element.
  • array (optional): source array.
  • initialValue (optional): The initial value of the accumulator. If not specified, the first element of the array will be used as the initial value.
let numbers = [1, 2, 3, 4, 5];
let sum = numbers.reduce(function(accumulator, currentValue) {
  return accumulator + currentValue;
}, 0); // the initial value of the accumulator is 0

console.log(sum); // 15

split() – split a string into an array of elements using a given delimiter.

let str = "apple,banana,orange";
let array = str.split(',');

console.log(array); // ["apple", "banana", "orange"]

concat() – used to combine two or more arrays and create a new array containing all the elements of those arrays.

let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
let array3 = [7, 8, 9];

let newArray = array1.concat(array2, array3);

console.log(newArray); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
   var newArray = array.concat()//will simply create a new array
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation