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.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.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.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 |
