Tutorial by Examples

There are many ways to create arrays. The most common are to use array literals, or the Array constructor: var arr = [1, 2, 3, 4]; var arr2 = new Array(1, 2, 3, 4); If the Array constructor is used with no arguments, an empty array is created. var arr3 = new Array(); results in: [] Note...
Spread operator 6 With ES6, you can use spreads to separate individual elements into a comma-separated syntax: let arr = [1, 2, 3, ...[4, 5, 6]]; // [1, 2, 3, 4, 5, 6] // in ES < 6, the operations above are equivalent to arr = [1, 2, 3]; arr.push(4, 5, 6); The spread operator also act...
It is often necessary to generate a new array based on the values of an existing array. For example, to generate an array of string lengths from an array of strings: 5.1 ['one', 'two', 'three', 'four'].map(function(value, index, arr) { return value.length; }); // → [3, 3, 5, 4] 6 ['one...
The filter() method creates an array filled with all array elements that pass a test provided as a function. 5.1 [1, 2, 3, 4, 5].filter(function(value, index, arr) { return value > 2; }); 6 [1, 2, 3, 4, 5].filter(value => value > 2); Results in a new array: [3, 4, 5] Fi...
A traditional for-loop A traditional for loop has three components: The initialization: executed before the look block is executed the first time The condition: checks a condition every time before the loop block is executed, and quits the loop if false The afterthought: performed every time a...
The filter() method accepts a test function, and returns a new array containing only the elements of the original array that pass the test provided. // Suppose we want to get all odd number in an array: var numbers = [5, 32, 43, 4]; 5.1 var odd = numbers.filter(function(n) { return n % 2 !=...
To join all of an array's elements into a string, you can use the join method: console.log(["Hello", " ", "world"].join("")); // "Hello world" console.log([1, 800, 555, 1234].join("-")); // "1-800-555-1234" As you can see in ...
What are Array-like Objects? JavaScript has "Array-like Objects", which are Object representations of Arrays with a length property. For example: var realArray = ['a', 'b', 'c']; var arrayLike = { 0: 'a', 1: 'b', 2: 'c', length: 3 }; Common examples of Array-like Objects...
5.1 The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value. Array Sum This method can be used to condense all values of an array into a single value: [1, 2, 3, 4].reduce(function(a, b) { return a + b; }); ...
5.1 .some and .every allow a logical connective of Array values. While .some combines the return values with OR, .every combines them with AND. Examples for .some [false, false].some(function(value) { return value; }); // Result: false [false, true].some(function(value) { return value...
Two Arrays var array1 = [1, 2]; var array2 = [3, 4, 5]; 3 var array3 = array1.concat(array2); // returns a new array 6 var array3 = [...array1, ...array2] Results in a new Array: [1, 2, 3, 4, 5] Multiple Arrays var array1 = ["a", "b"], array2 = ["...
Unshift Use .unshift to add one or more items in the beginning of an array. For example: var array = [3, 4, 5, 6]; array.unshift(1, 2); array results in: [1, 2, 3, 4, 5, 6] Push Further .push is used to add items after the last currently existent item. For example: var array = [1, 2, 3...
var object = { key1: 10, key2: 3, key3: 40, key4: 20 }; var array = []; for(var people in object) { array.push([people, object[people]]); } Now array is [ ["key1", 10], ["key2", 3], ["key3", 40], ["key4", 20] ] ...
Given the following array var array = [ ["key1", 10], ["key2", 3], ["key3", 40], ["key4", 20] ]; You can sort it sort it by number(second index) array.sort(function(a, b) { return a[1] - b[1]; }) 6 array.sort((a,b) => a[1] - b[1]);...
Shift Use .shift to remove the first item of an array. For example: var array = [1, 2, 3, 4]; array.shift(); array results in: [2, 3, 4] Pop Further .pop is used to remove the last item from an array. For example: var array = [1, 2, 3]; array.pop(); array results in: [1, 2] Bot...
.reverse is used to reverse the order of items inside an array. Example for .reverse: [1, 2, 3, 4].reverse(); Results in: [4, 3, 2, 1] Note: Please note that .reverse(Array.prototype.reverse) will reverse the array in place. Instead of returning a reversed copy, it will return the same ar...
When you need to remove a specific value from an array, you can use the following one-liner to create a copy array without the given value: array.filter(function(val) { return val !== to_remove; }); Or if you want to change the array itself without creating a copy (for example if you write a fun...
Array.isArray(obj) returns true if the object is an Array, otherwise false. Array.isArray([]) // true Array.isArray([1, 2, 3]) // true Array.isArray({}) // false Array.isArray(1) // false In most cases you can instanceof to check if an object is an Array. []...
The .sort() method sorts the elements of an array. The default method will sort the array according to string Unicode code points. To sort an array numerically the .sort() method needs to have a compareFunction passed to it. Note: The .sort() method is impure. .sort() will sort the array in-place...
Sometimes, you need to work with an array while ensuring you don't modify the original. Instead of a clone method, arrays have a slice method that lets you perform a shallow copy of any part of an array. Keep in mind that this only clones the first level. This works well with primitive types, like n...

Page 1 of 2