JavaScript. Methods for working with arrays

Methods for working with arrays. Method at(). Methods that define information about the array as a whole: every(), some(), includes()


Contents


Search on other Web resources:

1. Methods (functions) for working with arrays. General usage syntax. The list

JavaScript uses a wide range of methods to work with arrays. These methods allow you to conveniently operate on array elements.

Methods are called based on a common syntax

obj.FuncName(parameters)

where

  • obj – the name of an array instance created in a literal way (square brackets []) or using the new Array() operator;
  • FuncName – name of a method (function) for working with the array;
  • parameters – the list of parameters.

Below is a list of JavaScript methods for working with arrays and their interpretation:

  • concat() – returns a new array containing the current array and padded with other arrays and elements;
  • every() – returns true if every element of the array satisfies the condition specified by the test function. Otherwise, false is returned;
  • filter() – returns a new array containing all elements of the current array according to the condition specified in the test function;
  • forEach() – executes the specified function for each element of the array;
  • indexOf() – returns the index of the first occurrence of the given value in the array;
  • join() – combines all array elements into one string;
  • lastIndexOf() – returns the index of the last occurrence of the given value in the array;
  • map() – returns a new array, which is formed based on the specified transformation function;
  • pop() – removes (pulls out) the last element from the array;
  • push() – adds a new element to the end of the array;
  • reduce() – reduces two array values into one. For each value, the specified function is applied (from left to right);
  • reduce() – reduces two array values into one. For each value, the specified function is applied (from left to right);
  • reverse() – forms array elements in reverse order;
  • shift() – returns the first element from the array and simultaneously removes (pulls out) it (performs element shifting);
  • slice() – returns an array that is part of another source array;
  • some() – returns true if one or more elements satisfy the condition provided by the test function;
  • sort() – returns a sorted array. By default, the array is sorted alphabetically and in ascending order;
  • splice() – returns a new array consisting of elements added or removed from the given array;
  • toString() – converts the array to a string;
  • unShift() – returns a new array to which one or more elements have been added. The array length changes.

 

2. The at() method. Get array element by index

The at() method is used to get the element of an array at a given index.

item = array.at(index)

where

  • array – source array;
  • index – the position of the element in the array;
  • item – the received element.

Calling the at() method is equivalent to calling

item = array[index]

Example.

// Method at().Get array element by index.

// 1. The original array of numbers
var A = new Array( 0, 1, 2, 3, 4, 5, 6, 7 )
var itemA = A.at(3) // itemA = 3
console.log("itemA = " + itemA)

// 2. Array of strings
var S = [ "abcd", "efgh", "ijklm", "noprst" ]
var itemS = S.at(2) // itemS = ijklm
console.log("itemS = " + itemS)

Result

itemA = 3
itemS = ijklm

 

3. Method every(). Determine if each element of the array satisfies a condition

The every() method determines whether each element of the array satisfies some condition that is given by the function. If all elements of the array satisfy some condition, the every() method returns true. Otherwise, the method returns false.

A simplified way to use the every() method in a program could be like this

res = array.every(Func)

where

  • array – initial array of objects being processed;
  • Func – name of a function that returns true or false;
  • res – result of boolean type (true, false).

Continuing the topic, the Func() function can take up to 3 parameters. The declaration of a function with 3 parameters is

function Func(value, index, array)
{
  // The body of a function that returns a boolean value (true or false)
  // ...
}

where

  • value – value of the array element being processed;
  • index – position of the value element in the array array. So value == array.index;
  • array – processed array of elements.

The Func() function can also be implemented with two and one parameters:

// Declaration with 2 parameters
function Func(value, index)
{
  // ...
}

// Declaration with 1 parameter,
// this is the case when value is sufficient
function Func(value)
{
  // ...
}

Example.

In the example, using the every() function, the following is defined:

  • are all elements of the array of integers positive (>=0);
  • whether all strings in the array have a length of 0 to 3 characters (no more than 3 characters).

 

// Function every().
// Determine if each element of the array satisfies a condition
// 1. For array of integers
// 1.1. Declare array of numbers
var A1 = [2, 8, 3, 5, 4, 1, 8, 3, 10, 0 ];

// 1.2. Declare a function that returns true
// if the array element is not negative (>=0).
function IsPositive(value, index, array) {
  return array[index] >= 0
}

// 1.3. Create a new array and output it
var res = A1.every(IsPositive);
console.log("res = " + res)

// 2. For array of strings.
// 2.1. Declare the array of strings
var S1 = ["abd", "abc", "abd", "bd", "jklmn", "j"]

// 2.2. Declare a function that returns true
// if the string has a length of at most 3 characters (0..3).
function IsLength_1_3(value) {
  if (value.length <= 3) return true;
  else return false;
}

// 2.3. Call the every() function to determine
// if all strings are between 0 and 3 characters long.
res = S1.every(IsLength_1_3)
console.log("res = " + res)

console.log("Ok");

Result

res = true
res = false
Ok

 

4. Method some(). Determine if at least one element of the array satisfies a given condition

The some() method returns true if there is at least one element in the given array that satisfies the given condition. The condition is specified by the function. Using the method in the program can be like this

result = array.some(Func)

where

  • array – the array being viewed;
  • result – the result of a boolean type, which takes the value true or false;
  • Func is the name of the function that determines whether an element meets a given criterion (a given condition).

The Func() function has something like the following usage

function Func(value, index, array) {
  // Performing actions that return true or false
  // ...
}

where

  • value – current value of the considered array;
  • index – position of the current value of the array. So array[index]==value;
  • array is the name of the array in question.

The Func() function can be used with a single value parameter if that’s enough to define the selection criteria

function Func(value) {
  // Performing actions that return true or false
  // ...
}

Example.

// The some() method. Determine if at least one element of the array
// matches the condition specified by the function.

// 1. Processing an array of integers
// Determine if at least one element of the array is equal to the value 30.
// 1.1. Declare initial array of numbers
var A = new Array(10, 1, -2, 30, -4, 5, 26, 17)

// 1.2. Form a function that returns true or false
function Is_Equal_30(value, index, array) {
  return array[index] == 30
}

// 1.3. Calling the some() method
var res = A.some(Is_Equal_30)

// 1.4. Display the result
console.log("res = " + res) // res = true

// 2. Processing a string
// Determine if the given array of strings contains the word "abc"
// 2.1. Given an array of strings
var S = ["ac", "acb", "bca", "bcd", "bcb", "aba", "abc"]

// 2.2. Declare a function Is_Plus() that returns true
// if the array element is the word "abc"
function Is_ABC(value) {
  return value == "abc"
}

// 2.3. Determine if the array S contains the word "abc"
res = S.some(Is_ABC)

// 2.4. Display the result
console.log("res = " + res)

Result

res = true
res = true

 

5. Method includes(). Determine the presence of a given element in the array

The includes() function determines if the given element is present in the array. If the element is present, then the function returns true, otherwise the function returns false. The use of the function in the program can be like this

f_is = array.includes(item)

where

  • array – an array in which the element is searched;
  • f_is – result;
  • item – the element to search for.

Example.

// Method includes().
// Determine if the given element is in the array.

// 1. The original array of numbers
var A1 = new Array( 10, 21, -12, 33, -41, 55, 60, 87 )

// 2. Call the function
var res = A1.includes(33)

// 3. Display the result
console.log("A1 = " + A1)
console.log("res = " + res)

Result

A1 = 10,21,-12,33,-41,55,60,87
res = true

 


Related topics