Array
, develop along with the other mechanisms of the language. They are lists of numbered values. const a = [] const a = [1, 2, 3] const a = Array.of(1, 2, 3) const a = Array(6).fill(1) // , 6 , 1
const a = [1, 2, 3] console.log(a) //[ 1, 2, 3 ] const first = a[0] console.log(first) //1 a[0] = 4 console.log(a) //[ 4, 2, 3 ]
Array
constructor is not recommended for declaring arrays. const a = new Array() // const a = new Array(1, 2, 3) //
length
property. const l = a.length
every()
method of arrays can be used to organize the inspection of all their elements using some condition. If all the elements of the array match the condition, the function will return true
; otherwise, it will return false
.currentValue
(the current element of the array), index
(the index of the current element of the array), and array
(the array itself). It can also take the optional value used as this
when executing the function passed to it. const a = [11, 12, 13] const b = [5, 6, 25] const test = el => el > 10 console.log(a.every(test)) //true console.log(b.every(test)) //false
test()
function, we are only interested in the first argument passed to it, so we declare it, indicating only the parameter el
, into which the corresponding value will fall.every()
method, but it returns true
if at least one of the array elements satisfies the condition given by the function passed to it.map()
method of arrays allows you to iterate over arrays, applying to each of their elements passed to this method a function that converts an element, and create new arrays from the obtained values. Here, for example, how to get a new array, which is the result of multiplying all the elements of the original array by 2. const a = [1, 2, 3] const double = el => el * 2 const doubleA = a.map(double) console.log(a) //[ 1, 2, 3 ] console.log(doubleA) //[ 2, 4, 6 ]
filter()
method is similar to the map()
method, but it allows you to create new arrays containing only those elements of the original arrays that satisfy the condition specified by the function passed to the filter()
method.reduce()
method allows you to apply a given function to the battery and to each value of the array, reducing the array to a single value (this value can be either a primitive or an object type). This method takes the function that performs the conversion and the optional initial value of the battery. Consider an example. const a = [1, 2, 3, 4].reduce((accumulator, currentValue, currentIndex, array) => { return accumulator * currentValue }, 1) console.log(a) //24 // 1: 1 * 1 = 1 // 2: 1 * 2 = 2 // 3: 2 * 3 = 6 // 4: 6 * 4 = 24
forEach()
can be used to iterate over the values ​​of arrays and to perform on them some actions specified by the function passed to the method. For example, print one by one the elements of an array into the console. const a = [1, 2, 3] a.forEach(el => console.log(el)) //1 //2 //3
forEach()
. Therefore, if in the course of solving a certain problem, you may need to interrupt the cycle, it is best to choose some other way to iterate over the elements of the array.for...of
operator appeared in the ES6 standard. It allows iterating objects to be iterated (including arrays). Here's how to use it. const a = [1, 2, 3] for (let v of a) { console.log(v) } //1 //2 //3
a
falls into the variable v
.for
statement allows you to organize loops, which, in particular, can be used to iterate (or initialize) arrays, referring to their elements by index. Usually, the index of the next element is obtained using the loop counter. const a = [1, 2, 3] for (let i = 0; i < a.length; i += 1) { console.log(a[i]) } //1 //2 //3
continue
command. For early termination of the cycle, you can use the break
command. If in a loop, for example, located in a certain function, use the return
command, the loop and the function will end, and the return
value using return
will go to where the function was called.Symbol.iterator
. After receiving an iterator, you can access its next()
method, which, each time it is called, returns a data structure containing the next element of the array. const a = [1, 2, 3] let it = a[Symbol.iterator]() console.log(it.next().value) //1 console.log(it.next().value) //2 console.log(it.next().value) //3
next()
method after the last element of the array is reached, it returns, as an element value, undefined
. The object returned by the next()
method contains the value
and done
properties. The done
property is set to false
until the last element of the array is reached. In our case, if it is called it.next()
for the fourth time, it will return the object { value: undefined, done: true }
, while for the three previous calls this object looked like { value: , done: false }
.entries()
array method returns an iterator that allows you to iterate over the key-value pairs of the array. const a = [1, 2, 3] let it = a.entries() console.log(it.next().value) //[0, 1] console.log(it.next().value) //[1, 2] console.log(it.next().value) //[2, 3]
keys()
method allows you to iterate over the keys of an array. const a = [1, 2, 3] let it = a.keys() console.log(it.next().value) //0 console.log(it.next().value) //1 console.log(it.next().value) //2
push()
method. a.push(4)
unshift()
method. a.unshift(0) a.unshift(-2, -1)
pop()
method. a.pop()
shift()
method, you can remove an element from the beginning of an array. a.shift()
splice()
method. a.splice(0, 2) // 2 a.splice(3, 2) // 2 , 3
splice()
method already familiar to you. const a = [1, 2, 3, 4, 5, 6] a.splice(2, 3, 'a', 'b') console.log(a) //[ 1, 2, 'a', 'b', 6 ]
concat()
method, which returns a new array. const a = [1, 2] const b = [3, 4] const c = a.concat(b) console.log(c) //[ 1, 2, 3, 4 ]
indexOf()
method, which returns the index of the first occurrence of the required array element. If the element in the array cannot be found, it returns -1
. const a = [1, 2, 3, 4, 5, 6, 7, 5, 8] console.log(a.indexOf(5)) //4 console.log(a.indexOf(23)) //-1
lastIndexOf()
method returns the index of the last occurrence of an element in an array, or, if no element is found, -1
. const a = [1, 2, 3, 4, 5, 6, 7, 5, 8] console.log(a.lastIndexOf(5)) //7 console.log(a.lastIndexOf(23)) //-1
find()
array method has appeared, which performs a search on an array using the function passed to it. If the function returns true
, the method returns the value of the first element found. If the item cannot be found, the function will return undefined
. a.find(x => x.id === my_id)
id
equals the given one.findIndex()
method is similar to find()
, but it returns the index of the item found or undefined
.includes()
method has appeared, which allows you to check for the presence of a certain element in the array. It returns true
or false
by finding or not finding the item of interest to the programmer. a.includes(value)
a.includes(value, i)
slice()
method. If this method is called without arguments, then the returned array will be a full copy of the original one. It takes two optional parameters. The first one sets the initial index of the fragment, the second - the final one. If the final index is not specified, the array is copied from the specified starting index to the end. const a = [1, 2, 3, 4, 5, 6, 7, 8, 9] console.log(a.slice(4)) //[ 5, 6, 7, 8, 9 ] console.log(a.slice(3,7)) //[ 4, 5, 6, 7 ]
0-9A-Za-z
), the sort()
method is used without passing arguments to it. const a = [1, 2, 3, 10, 11] a.sort() console.log(a) //[ 1, 10, 11, 2, 3 ] const b = [1, 'a', 'Z', 3, 2, 11] b.sort() console.log(b) //[ 1, 11, 2, 3, 'Z', 'a' ]
a
and b
. It returns a negative number if a
less than b
by some criterion, 0 if they are equal, and a positive number if a
greater than b
. When writing a similar function to sort numeric arrays, it can return the result of subtracting a
and b
. So, returning the result of evaluating the expression a - b
means sorting the array in ascending order; returning the result of evaluating the expression b - a
will result in sorting the array in descending order. const a = [1, 10, 3, 2, 11] console.log(a.sort((a, b) => a - b)) //[ 1, 2, 3, 10, 11 ] console.log(a.sort((a, b) => b - a)) //[ 11, 10, 3, 2, 1 ]
reverse()
method. It, like sort()
, modifies the array for which it is called.toString()
method. a.toString()
join()
method, called without arguments. a.join()
const a = [1, 10, 3, 2, 11] console.log(a.toString()) //1,10,3,2,11 console.log(a.join()) //1,10,3,2,11 console.log(a.join(', ')) //1, 10, 3, 2, 11
Array.from()
method. It is also suitable for creating arrays from array-like objects (from strings, for example). const a = 'a string' const b = Array.from(a) console.log(b) //[ 'a', ' ', 's', 't', 'r', 'i', 'n', 'g' ]
Array.of()
method can also be used to copy arrays, as well as to “build” arrays from various elements. For example, to copy the elements of one array to another, you can use the following construction. const a = [1, 10, 3, 2, 11] const b = Array.of(...a) console.log(b) // [ 1, 10, 3, 2, 11 ]
copyWithin()
method. Its first argument specifies the initial index of the target position, the second - the initial index of the position of the source of elements, and the third parameter, optional, specifies the final index of the position of the source of the elements. If you do not specify it, everything will be copied to the specified location in the array, starting from the initial index of the source position to the end of the array. const a = [1, 2, 3, 4, 5] a.copyWithin(0, 2) console.log(a) //[ 3, 4, 5, 4, 5 ]
const list = ['a', 'b', 'c'] for (let i = 0; i < list.length; i++) { console.log(list[i]) //, console.log(i) // }
break
command, and you can skip the current iteration and proceed directly to the next using the continue
command. const list = ['a', 'b', 'c'] list.forEach((item, index) => { console.log(item) // console.log(index) // }) // , list.forEach(item => console.log(item))
const list = ['a', 'b', 'c'] let i = 0 do { console.log(list[i]) // console.log(i) // i = i + 1 } while (i < list.length)
break
command, it is possible to proceed to its next iteration with the continue
command. const list = ['a', 'b', 'c'] let i = 0 while (i < list.length) { console.log(list[i]) // console.log(i) // i = i + 1 }
let object = {a: 1, b: 2, c: 'three'} for (let property in object) { console.log(property) // console.log(object[property]) // }
for...of
loop combines the convenience of the forEach
loop and the ability to interrupt its work with regular means. // for (const value of ['a', 'b', 'c']) { console.log(value) // } // `entries()` for (const [index, value] of ['a', 'b', 'c'].entries()) { console.log(index) // console.log(value) // }
const
, and not, as you would expect, let
. If the variables inside the loop block do not need to be reassigned, then const
us.for...in
and for...of
loops, then it turns out that for...in
iterates the property names, and for...of
property values.let
talk about cycles, scopes, and the var
and let
keywords. const operations = [] for (var i = 0; i < 5; i++) { operations.push(() => { console.log(i) }) } for (const operation of operations) { operation() }
operations
array. This function displays the value of the loop counter in the console - i
. After the functions are added to the array, we search through this array and call the functions that are its elements. 0 1 2 3 4
5 5 5 5 5
var
keyword. var i; const operations = [] for (i = 0; i < 5; i++) { operations.push(() => { console.log(i) }) } for (const operation of operations) { operation() }
for...of
loop, in which we iterate over the array, the variable i
is still visible, it is equal to 5, as a result, referring to i
in all functions, we derive the number 5.let
keyword. It, as we said, appeared in ES6, its use allows you to get rid of some of the oddities characteristic of var
.var
to let
and everything will work as it should. const operations = [] for (let i = 0; i < 5; i++) { operations.push(() => { console.log(i) }) } for (const operation of operations) { operation() }
operations
array gets its own copy of i
. Remember that in this situation you cannot use the keyword const
, since the value of i
in the cycle changes.let
did not exist, is to use IIFE.i
stored in the closure, and the function returned by IIFE and having access to the closure gets into the array. This function can be performed when the need arises. Here's what it looks like. const operations = [] for (var i = 0; i < 5; i++) { operations.push(((j) => { return () => console.log(j) })(i)) } for (const operation of operations) { operation() }
Source: https://habr.com/ru/post/430380/
All Articles