// Array-like object (arguments) to Array
function fooBar() {
return Array.from(arguments);
}
fooBar(1, 2, 3, 5); // [1, 2, 3, 5];
// Any iterable object...
// Set
var s = new Set(["foo", window]);
Array.from(s);
// ["foo", window]
// Map
var m = new Map([[1, 2], [2, 4], [4, 8]]);
Array.from(m);
// [[1, 2], [2, 4], [4, 8]]
// Using an arrow function as the map function to
// manipulate the elements
Array.from([1, 2, 3], x => x + x);
// [2, 4, 6]
// Array.fill allows you to replace all the elements of an array with the passed element
var randomNumbers = [1, 5, 7, 77, 12, 3];
randomNumbers.fill('*'); // ['*', '*', '*', '*', '*', '*'];
// Array.find receives a callback and returns the first element satisfying the condition in callback
var users = [{name: 'John Doe', age: 23}, {name: 'Jane Doe', age: 25}, {name: 'Kane Doe', age: 30}];
users.find(user => user.age > 25); // {name: 'Kane Doe', age: 30}
// Also a similar function is Array.findIndex which works the same but returns the index of the matching element instead.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.