Array
Creating Array
// Array Literal
let numbers = [1, 2, 4, 7];
console.log(numbers); //=> [ 1, 2, 4, 7 ]
// Array
let numbers = Array(1, 2, 4, 7);
console.log(numbers); //=> [ 1, 2, 4, 7 ]
// Array OO
let numbers = new Array(1, 2, 4, 7);
console.log(numbers); //=> [ 1, 2, 4, 7 ]
let numbers = new Array(3);
console.log(numbers); //=> [ , , ]
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
console.log(numbers); //=> [ 1, 2, 3 ]
Array Object
Array
Array.from()
Syntax:
Array.from(arrayLike)
Array.from(arrayLike, mapFn)
Array.from(arrayLike, mapFn, thisArg)
Array.from([1, 2, 3]); //=> [1, 2, 3]
Array Instances
Array.prototype.length
Syntax:
arr.length
console.log([1, 2, 3].length); //=> 3
Mutator: pop(), push(), reverse(), shift(), unshift(), sort(), splice()
Array.prototype.push()
Syntax:
arr.push(element1, element2,…)
let numbers = [1, 2, 3];
console.log(numbers.push(4)); //=> 4
console.log(numbers); //=> [ 1, 2, 3, 4 ]
Array.prototype.unshift()
Syntax:
arr.unshift(element1, element2,…)
let numbers = [1, 2, 3];
console.log(numbers.unshift(0)); //=> 4
console.log(numbers); //=> [ 0, 1, 2, 3 ]
Array.prototype.pop()
Syntax:
arr.pop()
let numbers = [1, 2, 3];
console.log(numbers.pop()); //=> 3
console.log(numbers); //=> [ 1, 2 ]
Array.prototype.shift()
Syntax:
arr.shift()
let numbers = [1, 2, 3];
console.log(numbers.shift()); //=> 1
console.log(numbers); //=> [ 2, 3 ]
Array.prototype.reverse()
Syntax:
arr.reverse()
let numbers = [1, 2, 3];
console.log(numbers.reverse()); //=> [ 3, 2, 1 ]
console.log(numbers); //=> [ 3, 2, 1 ]
Array.prototype.sort()
Syntax:
arr.sort()
arr.sort(compareFunction)
let numbers = [3, 1, 2];
console.log(numbers.sort()); //=> [ 1, 2, 3 ]
console.log(numbers); //=> [ 1, 2, 3 ]
References: Merge Sort, Selection Sort
> Javascript Array.sort implementation?
> Algoritmos de ordenação e o seu JavaScript
> Sorting algorithms in JavaScript
> Programming with JS: Merge Sort
Array.prototype.splice()
Syntax:
arr.splice(start)
arr.splice(start, deleteCount)
arr.splice(start, deleteCount, item1, item2, …)
let numbers = [1, 2, 3];
console.log(numbers.splice(1, 2)); //=> [ 2, 3 ]
console.log(numbers); //=> [1]
Accessor: includes(), join(), concat(), slice()
Array.prototype.includes()
Syntax:
arr.includes(searchElement)
arr.includes(searchElement, fromIndex)
console.log([1, 2, 3].includes(1)); //=> true
Array.prototype.join()
Syntax:
arr.join()
arr.join(separator)
console.log([1, 2, 3].join(' ')); //=> '1 2 3'
Array.prototype.slice()
Syntax:
arr.slice()
arr.slice(begin)
arr.slice(begin, end)
console.log([1, 2, 3].slice(1, 2)); //=> 2
Array.prototype.concat()
Syntax:
var new_array = old_array.concat(value1, value2, …)
console.log([1, 2].concat(['hello', true, 7])); //=> [ 1, 2, 'hello', true, 7 ]
Iteration: forEach(), reduce(), filter(), map(), every(), some(), find(), reduceRight(), entries(), keys(), values()
References:
Array.prototype.map()
Syntax:
var new_array = arr.map(callback) var new_array = arr.map(callback, thisArg)
callback(currentValue, currentIndex, array)
Double Number
f(x) = 2x
Input vs Output
x | f(x) |
---|---|
1 | 2 |
2 | 4 |
3 | 6 |
4 | 8 |
5 | 10 |
6 | 12 |
Diagram
Code
let array = [1, 2, 3, 4, 5, 6];
// f(x) = 2x
console.log(array.map(value => value * 2)); //=> [ 2, 4, 6, 8, 10, 12 ]
Array.prototype.filter()
Syntax:
let new_arr = old_arr.filter(callback)
let new_arr = old_arr.filter(callback, thisArg)
callback(currentValue, currentIndex, array)
Odd Number
f(x) = x % 2 != 0
Input vs Output
x | f(x) |
---|---|
1 | true |
2 | false |
3 | true |
4 | false |
5 | true |
6 | false |
Diagram
Code
let array = [1, 2, 3, 4, 5, 6];
console.log(array.filter(value => value % 2 !== 0)); //=> [ 1, 3, 5 ]
Double Odd Number
f(x) = x % 2 != 0
g(x) = 2x
Input vs Output
x | f(x) | g(x) |
---|---|---|
1 | true | 2 |
2 | false | |
3 | true | 6 |
4 | false | |
5 | true | 10 |
6 | false |
Diagram
Code
let array = [1, 2, 3, 4, 5, 6];
const result = array.filter(value => value % 2 !== 0).map(e => 2 * e);
console.log(result); //=> [ 2, 6, 10 ]
Array.prototype.reduce()
Reference: doc
Syntax:
arr.reduce(callback)
arr.reduce(callback, initialValue)
>
callback(accumulator, currentValue, currentIndex, array)
Sum Array (zero)
f(x, y) = x + y
Input vs Output
x | y | f(x, y) |
---|---|---|
0 | 1 | 1 |
1 | 2 | 3 |
3 | 3 | 6 |
6 | 4 | 10 |
10 | 5 | 15 |
15 | 6 | 21 |
Diagram
Code
let array = [1, 2, 3, 4, 5, 6];
console.log(array.reduce((addition, value) => addition + value, 0)); //=> 21
Sum Array
f(x, y) = x + y
Input vs Output
x | y | f(x, y) |
---|---|---|
1 | 2 | 3 |
3 | 3 | 6 |
6 | 4 | 10 |
10 | 5 | 15 |
15 | 6 | 21 |
Diagram
Code
let array = [1, 2, 3, 4, 5, 6];
console.log(array.reduce((addition, value) => addition + value)); //=> 21
Product
let array = [1, 2, 3, 4, 5, 6];
console.log(array.reduce((addition, value) => addition * value), 1); //=> 720
Array.prototype.reduceRight()
Syntax:
arr.reduceRight(callback)
arr.reduceRight(callback, initialValue)
callback(currentValue, currentIndex, array)
let array = [1, 2, 3, 4, 5, 6];
console.log(array.reduceRight((addition, value) => addition + value)); //=> 21
Array.prototype.forEach()
Syntax:
arr.forEach(callback) arr.forEach(callback, thisArg)
callback(currentValue, currentIndex, array)
[1, 2, 3]
.forEach(function(value) {
console.log(value);
})
[(1, 2, 3)].forEach(value => console.log(value))
[(1, 2, 3)].forEach(value => console.log(value));
//=>
// '1'
// '2'
// '3'
[1, 2, 3]
.forEach(function(value, index) {
console.log(value + ' ' + index);
})
[(1, 2, 3)].forEach((value, index) => console.log(value + ' ' + index));
//=>
// '0 => 1'
// '1 => 2'
// '2 => 3'
Array.prototype.entries()
Syntax:
arr.entries()
for ([key, value] of [1, 2, 3].entries()) {
console.log(`${key} => ${value}`);
}
//=>
// '0 => 1'
// '1 => 2'
// '2 => 3'
Array.prototype.find()
Syntax:
arr.find(callback)
arr.find(callback, thisArg)
callback(currentValue, currentIndex, array)
let array = [1, 2, 3, 4, 5, 6];
console.log(array.find(value => value % 2 == 0)); //=> 2
Array.prototype.every()
Syntax:
arr.every(callback)
arr.every(callback, thisArg)
callback(currentValue, currentIndex, array)
let array = [1, 2, 3, 4, 5, 6];
console.log(array.every(value => value % 2 == 0)); //=> false
let array = [2, 4, 6];
console.log(array.every(value => value % 2 == 0)); //=> true
Array.prototype.some()
Syntax:
arr.some(callback)
arr.some(callback, thisArg)
callback(currentValue, currentIndex, array)
let array = [1, 2, 3, 4, 5, 6];
console.log(array.some(value => value % 2 == 0)); //=> true