Array.from()
Using the method “from()” we can make an array from “array-like” or “iterable”:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// String to array Array.from('foo'); // Returns: ["f", "o", "o"] // Set to array var s = new Set(['foo', window]); Array.from(s); // Returns: ["foo", window] // Map to array var m = new Map([[1, 2], [2, 4], [4, 8]]); Array.from(m); // Returns: [[1, 2], [2, 4], [4, 8]] // Array-like to array function f() { return Array.from(arguments); } f(1, 2, 3); // Returns: [1, 2, 3] |
Array.prototype.concat()
Joins two or more arrays, and returns a copy of the joined arrays
Array.prototype.copyWithin()
Copies array elements within the array, to and from specified positions
Array.prototype.every()
This method is used to test the array, it checks if all members of the array satisfy a condition (a condition within the callback function) and returns a boolean value. The method does not modify the source string. Three parameters are passed to the callback function of the method:
- string member value
- index of array member
- an array (on which the method is executed)
In addition to the callback function, the argument thisArg can (optionally) be passed, which defines what the reserved word this will point to within the callback function.
|
1 2 3 |
izvorniNiz.every(function (currentValue, index, array) { // some condition }, [thisArg]); |
Example
|
1 2 3 4 5 6 7 8 9 10 |
var osobe = [19, 21, 32, 16, 35], istinitost = osobe.every(function(item, index, array){ return item>=18; }); // ReturnsTrue ili False if (istinitost){ console.log("Svi su punoletni"); } else { console.log ("Neko nije punoletan!"); } |
Array.prototype.fill()
Fill the elements in an array with a static value
Array.prototype.filter()
This method also returns a new array, but only with elements that passed the test implemented in the selected function. The method does not modify the source string. Three parameters are passed to the callback function of the method:
- string member value
- index of array member
- an array (on which the method is executed)
In addition to the callback function, the argument thisArg can (optionally) be passed, which defines what the reserved word this will point to within the callback function.
|
1 2 3 |
var noviNiz = izvorniNiz.filter(function (currentValue, index, array) { // some condition }, [thisArg]); |
Example
|
1 2 3 4 5 6 |
function nadjiPozitivne(broj) { return broj>0; } var pozitivniBrojevi = [-109, 12, -5, 38, 44].filter(nadjiPozitivne); console.log(pozitivniBrojevi); // Returns: [12, 38, 44] |
Example
|
1 2 3 4 5 |
var a = [1, 2, 3, 4, 5]; var neparniClanoviNiza = a.filter(function(clan,index) { return index%2===0; }); console.log(neparniClanoviNiza); // Returns: [1, 3, 5] |
Example
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
var users = [ { "name": "Pera", "age": 30, "email": "pera@gmail.com" }, { "name": "Marija", "age": 20, "email": "marija@gmail.com" }, { "name": "Mirko", "age": 19, "email": "mirko@gmail.com" }, { "name": "Milos", "age": 39, "email": "milos@gmail.com" } ]; // Filtering returns: an array of objects that satisfy the condition var sortedByAge = users.filter(function(el){ return el.age >= 30; }); // Printing user names from objects that met the condition sortedByAge.forEach(function(element) { console.log(element.name); }); // Returns: "Pera" "Milos" |
Array.prototype.find()
This method returns the first member of the array that satisfies the condition (the condition within the callback function). The method passes three parameters to the callback function:
- string member value
- index of array member
- an array (on which the method is executed)
In addition to the callback function, the argument thisArg can (optionally) be passed, which defines what the reserved word this will point to within the callback function.
|
1 2 3 |
izvorniNiz.find(function (currentValue, index, array) { // some condition }, [thisArg]); |
Example
|
1 2 3 4 5 6 7 8 9 10 11 |
var korpa = [ {imeVoca: 'banana', kolicina: 0}, {imeVoca: 'an apple', kolicina: 2}, {imeVoca: 'visnja', kolicina: 5} ]; function daliImaVoca(vocka) { return vocka.kolicina > 0 ; // Returns: {fruit name: 'apple', quantity: 2} } var poslastice = korpa.find(daliImaVoca); console.log("Korpa nije prazna, ima " + poslastice.imeVoca); // Returns: Basket is not empty, there are apples |
The findIndex() method does a similar thing, except that instead of the first element that met the condition, it returns its index instring.
Array.prototype.findIndex()
This method returns the index of the first element in the array that satisfies the test function, if there is no such element then it returns “-1”.
Example
|
1 2 3 4 5 |
var array1 = [5, 12, 8, 130, 44]; function findFirstLargeNumber(element) { return element > 13; } console.log(array1.findIndex(findFirstLargeNumber)); // Returns: 3 |
Array.prototype.forEach()
This method executes a specific function (callback) using each element of the passed array. This function is less flexible than the standard “for loop” because it has no conditional statements and there is no way to terminate a “loop” once started other than throwing an exception. The method passes three parameters to the callback function:
- string member value
- index of array member
- an array (on which the method is executed)
|
1 2 3 |
someArray.forEach(function (currentValue, index, array) { // some calculation }, [thisArg]); |
In addition to the callback function, the argument thisArg can (optionally) be passed, which defines what the reserved word this will point to within the callback function. The function itself does not automatically return anything, so it is not “chainable” (the next method cannot be chained to it). Even after explicitly using the return statement, it will not return anything after the iteration.
Example
|
1 2 3 4 5 |
var someArray = ['a', 'b', 'c']; someArray.forEach(function(element) { console.log(element); }); // Returns: "a" "b" "c" |
The same result can be obtained using other attributes
|
1 2 3 4 5 |
var someArray = ['a', 'b', 'c']; someArray.forEach(function(element, index, prosledjen_niz) { console.log(prosledjen_niz[index]); }); // Returns: "a" "b" "c" |
Example
Callback function cannot use “empty array members” (array member with an index that has no associated value) in the calculation. Therefore, in the following example, this method will not take into account an empty element, so the array member with index 2 will be skipped:
|
1 2 3 4 5 |
function callbackFunkcija(element, index, array) { console.log('a[' + index + '] = ' + element); } [2, 5, , 9].forEach(callbackFunkcija); // Returns: a[0] = 2 a[1] = 5 a[3] = 9 |
Array.prototype.from()
Using the method “from()” we can make an array from “array-like” or “iterable”:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// String to array Array.from('foo'); // Returns: ["f", "o", "o"] // Set to array var s = new Set(['foo', window]); Array.from(s); // Returns: ["foo", window] // Map to array var m = new Map([[1, 2], [2, 4], [4, 8]]); Array.from(m); // Returns: [[1, 2], [2, 4], [4, 8]] // Array-like to array function f() { return Array.from(arguments); } f(1, 2, 3); // Returns: [1, 2, 3] |
Array.prototype.find()
This method returns the first member of the array that satisfies the condition (the condition within the callback function). The method passes three parameters to the callback function:
- string member value
- index of array member
- an array (on which the method is executed)
In addition to the callback function, the argument thisArg can (optionally) be passed, which defines what the reserved word this will point to within the callback function.
|
1 2 3 |
izvorniNiz.find(function (currentValue, index, array) { // some condition }, [thisArg]); |
Example
|
1 2 3 4 5 6 7 8 9 10 11 |
var korpa = [ {imeVoca: 'banana', kolicina: 0}, {imeVoca: 'an apple', kolicina: 2}, {imeVoca: 'visnja', kolicina: 5} ]; function daliImaVoca(vocka) { return vocka.kolicina > 0 ; // Returns: {fruit name: 'apple', quantity: 2} } var poslastice = korpa.find(daliImaVoca); console.log("Korpa nije prazna, ima " + poslastice.imeVoca); // Returns: Basket is not empty, there are apples |
The findIndex() method does a similar thing, except that instead of the first element that met the condition, it returns its index in the array.
Array.prototype.indexOf()
Search the array for an element and returns its position
Array.prototype.includes()
The method that arrived with the ES2016 standard a checks if the requested element exists in the array.
|
1 2 3 4 5 6 |
var array1 = [1, 2, 3]; console.log(array1.includes(2)); // Returns: true var pets = ['cat', 'dog', 'bat']; console.log(pets.includes('cat')); // Returns: true console.log(pets.includes('at')); // Returns: false |
Array.prototype.isArray()
Checks whether an object is an array
Array.prototype.join()
Joins all elements of an array into a string
Array.prototype.lastIndexOf()
Search the array for an element, starting at the end, and returns its position
Array.prototype.map()
This method returns a new array, which is obtained by using a callback function on the elements of the original array paying attention to the order of the members of the original array. As with the forEach() function, the callback function does not use “empty string members” (a string member with an index that has no associated value) in the calculation. The map() function does not affect the source string. The method passes three parameters to the callback function:
- string member value
- string member value
- index of array member
- an array (on which the method is executed)
|
1 2 3 |
var noviNiz = izvorniNiz.map(function (currentValue, index, array) { // some condition }, [thisArg]); |
In addition to the callback function, the argument thisArg can (optionally) be passed, which defines what the reserved word this will point to within the callback function.
Example
|
1 2 3 4 5 6 |
var izvorniNiz = [1, 2, 3]; var noviNiz = izvorniNiz.map(function (clanNiza){ return clanNiza * 10; }); console.log (noviNiz); // Returns: [10, 20, 30] |
Example
|
1 2 3 4 5 6 7 8 9 10 11 |
var osobe = [ {ime : "Pera", prezime: "Radic"}, {ime : "Mika", prezime: "Maric"}, {ime : "Zika", prezime: "Peric"} ]; function napraviPunoIme(osoba,index) { var punoIme = [osoba.ime,osoba.prezime].join(" "); return punoIme; } console.log(osobe.map(napraviPunoIme)); |
Pay attention to the case when functions that require clearly defined parameters are used for callback functions, and not the mentioned three parameters passed by the map() function.
The best example is the parseInt() function, which requires a string for the first parameter and a numeric system for the second.
|
1 2 |
var noviNiz = ["5", "2", "8"].map(parseInt); console.log(noviNiz); // Returns: [5, NaN, NaN] |
The parseInt() function returns NaN when it receives an array index from the map() method for the second parameter (which looks for a numeric system type, expected numbers are: 2, 8, 10, 16…).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
a=parseInt("5",0); console.log(a); // Returns: 5 b=parseInt("5",1); console.log(b); // Returns: NaN c=parseInt("5",2); console.log(c); // Returns: NaN . . . d=parseInt("5",10); console.log(d); // Returns: 5 |
