const arr = [10, 20, 30, 40, 50]
for(let i = 0; i < arr.length; i++) {
console.log(arr[i])
}const arr = [10, 20, 30, 40, 50]
for(let element of arr) {
console.log(element)
}
const obj = {
property_1: '1',
property_2: '2',
property_3: '3'
}
for(let key in obj) {
console.log(`Key: ${key} -> value: ${obj[key]}`)
}Array.prototype
Mutation methods
splice(start[, deleteCount[, item1[, item2[, ...]]]])
-> return array of deleted elements
reverse()
fill(value[, start[, end]])
Mutation methods
const numbers = [5, 3, 4, 1, 2]
const sortedNumbers = numbers.sort()
console.log(sortedNumbers) -> [1, 2, 3, 4, 5]
// !!!!
console.log(sortedNumbers === numbers) -> true
console.log(numbers) -> [1, 2, 3, 4, 5]
// !!!!
Mutation methods (sort part 2)
const numbers = [5, 3, 4, 10, 2]
const callback = (a, b) => (a - b)
const sortedNumbers = numbers.sort(callback)
console.log(sortedNumbers) -> [2, 3, 4, 5, 10]
const numbers = [5, 3, 4, 10, 2]
const sortedNumbers = numbers.sort()
console.log(sortedNumbers)
-> [10, 2, 3, 4, 5]callback return:
Non-mutating methods
slice([begin[, end]]) -> return array of elements from [begin, end)
concat([value1[, value2[, ...[, valueN]]]])
Iteration methods
callback(
element,
index,
array
)
Searching methods
callback(
element,
index,
array
)
Reduce method
callback(
accumulator
element,
index,
array
)
const numbers = [1, 2, 3, 4, 5]
let callsNumber = 0
const callback = (acc, element, index, array) => {
callsNumber++
return acc + element
}
const result = numbers.reduce(callback, 0)
console.log(result) -> 15
console.log(callsNumber) -> 5
callsNumber = 0
const result = numbers.reduce(callback)
console.log(result) -> 15
console.log(callsNumber) -> 4const arr = [10, 20, 30, 40, 50]
const [first, second, ...rest] = arr
console.log('first', first)
console.log('second', second)
console.log('rest', rest)const [first = 'defaul first', second = 'default second'] = []
console.log('first: ', first)
console.log('second: ', second)Object.prototype
const obj = {
'key1': 1,
'key2': 2,
'key3': 3
}
console.log( Object.keys(obj) ) -> ["key1", "key2", "key3"]
console.log( Object.values(obj) ) -> [1, 2, 3]
console.log( Object.entries(obj) ) -> [['key1', 1], ['key2', 2], ['key3', 3]]Check that object empty
const obj1 = {}
const obj2 = {
el: '1'
}
const isEmptyObj = (obj) => {
return Object.keys(obj).length === 0
}
console.log( isEmptyObj(obj1) ) -> true
console.log( isEmptyObj(obj2) ) -> falseconst key1 = { el: 1 }
const key2 = { el: 2 }
const obj = {
[key1]: 'key1 property',
[key2]: 'key2 property'
}
console.log( obj[key1] ) -> ???const key1 = { el: 1 }
const key2 = { el: 2 }
const obj = {
[key1]: 'key1 property',
[key2]: 'key2 property'
}
console.log( obj[key1] ) -> 'key2 property'Map is a collection of keyed data items, just like an Object. But the main difference is that Map allows keys of any type.
A Set is a special type collection – “set of values” (without keys), where each value may occur only once.
We can loop over a set either with for..of or using forEach
const arr = [1, 2, 3, 1]
const set = new Set(arr)
const uniqArr = new Array(...set)
console.log(arr) -> [1, 2, 3, 1]
console.log(uniqArr) -> [1, 2, 3]