ECMA Script 6 Array

I have compiled a list of methods that have been used by the ECMA Script 6 Array in this article, hope you will like it.

Array.from :

Below is a simple example of the usage of the Array.from method which will create a new array instance with the word “hello” in front of every element of the old array.

let greeting = ["world", "man", "you"];

let hello = {hi : "hello"}

let arry = Array.from(greeting, function(value) {
	return this.hi + " " + value;
}, hello);

The result is [ “hello world”, “hello man”, “hello you” ]

The full function is as follow:-

Array.from(iterable, mapFunc, this);

Array.of :

Array.of will create a new Array instance from a group of elements, for example below script will create an array of 5 elements.

Array.of(1, 2, 3, 4, 5);   // [1, 2, 3, 4, 5]

If you want to create a more advance feature then you can always use Array.from instead.

Array.join :

The ECMAscript build in Array.join method will join up an array with the separator provides in the method.

(function() {
var a = ['hello', 'the', 'world'];
var hello = a.join(', ');

alert(hello); // hello, the, world
})();

Array.prototype.find :

Here is an example on how to use Array.prototype.find() which has been introduced in ECMAScript 6 as an array method to find an element in the array.

function theFirstEven(number) {
  return !(number % 2); // find even number
}

alert([3, 5, 7, 6, 57].find(theFirstEven));

The above script will output number 6 which is an even number within that array.

array Destructuring :

ECMA script 6 has allowed us to assign an array element directly to a variable just like how we did it in python. Here is an example,

let [a, b, c] = [2, 3, 5];

console.log(a + b + c);

The right hand side array’s elements will get assigned to the left hand side variables based on the order of both the array’s elements and the variables. The above outcome is 10!

The use of … token in the array destructuring expression :

let [a, …b] = [0, 1, 2, 3]

now b will become an array which contains [1,2,3].

Hope you like this post, more updates about array will come later…

Like this tutorial? Why not get those latest tutorial on programming, tech and web development by simply subscribe to this blog.

[rainmaker_form id=”308″]

Like this post? Kindly share!