ZeptoBook

ZeptoBook

1567442774

Array.Prototype.Flat() or ES2019 Array Flat() function - ZeptoBook

#javascript

What is GEEK

Buddha Community

Array.Prototype.Flat() or ES2019 Array Flat() function - ZeptoBook

Javascript Array Flat Example | Array.prototype.flat()

Javascript Array flat() is an inbuilt method that creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. It returns a new array with the sub-array elements concatenated into it.

Javascript Array Flat Example

As its name suggests, the Array flat() method available on an Array prototype returns the new array that’s the flattened version of an array it was called on. If we do not provide arguments passed-in, then the depth of 1 is assumed.

The syntax of the Javascript Array flat() method is the following.

let newArray = arr.flat([depth]);

The depth level is specifying how deep a nested array structure should be flattened. Defaults to 1. It is an optional parameter.

#javascript #flat #array.prototype.flat #js

JavaScript tutorial - array.prototype.flat()

Javascript Array flat() is an inbuilt method that creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. It returns a new array with the sub-array elements concatenated into it.This tutorial covers array.prototype.flat() method. A multiple examples have been provided.

Javascript Array Flat Example

As its name suggests, the Array flat() method available on an Array prototype returns the new array that’s the flattened version of an array it was called on. If we do not provide arguments passed-in, then the depth of 1 is assumed.

The syntax of the Javascript Array flat() method is the following.

let newArray = arr.flat([depth]);

The depth level is specifying how deep a nested array structure should be flattened. Defaults to 1. It is an optional parameter.

Let’s see the following example.

// app.js

const arr = [['krunal', 'ankit'], [21, 74]];

console.log(arr.flat());

See the output.

Javascript Array Flat Example | Array.prototype.flat() Tutorial

In the above example, by default, the depth level is 1. Let’s see the example where depth level is more than 1.

// app.js

const arr = [['krunal', 'ankit'], [21, 74], ['appdividend', ['Laravel', 'Angular']]];

console.log(arr.flat(2));

#javascript #array #prototype #flat #programming

Vincent Lab

Vincent Lab

1605017502

The Difference Between Regular Functions and Arrow Functions in JavaScript

Other then the syntactical differences. The main difference is the way the this keyword behaves? In an arrow function, the this keyword remains the same throughout the life-cycle of the function and is always bound to the value of this in the closest non-arrow parent function. Arrow functions can never be constructor functions so they can never be invoked with the new keyword. And they can never have duplicate named parameters like a regular function not using strict mode.

Here are a few code examples to show you some of the differences
this.name = "Bob";

const person = {
name: “Jon”,

<span style="color: #008000">// Regular function</span>
func1: <span style="color: #0000ff">function</span> () {
    console.log(<span style="color: #0000ff">this</span>);
},

<span style="color: #008000">// Arrow function</span>
func2: () =&gt; {
    console.log(<span style="color: #0000ff">this</span>);
}

}

person.func1(); // Call the Regular function
// Output: {name:“Jon”, func1:[Function: func1], func2:[Function: func2]}

person.func2(); // Call the Arrow function
// Output: {name:“Bob”}

The new keyword with an arrow function
const person = (name) => console.log("Your name is " + name);
const bob = new person("Bob");
// Uncaught TypeError: person is not a constructor

If you want to see a visual presentation on the differences, then you can see the video below:

#arrow functions #javascript #regular functions #arrow functions vs normal functions #difference between functions and arrow functions

Javascript Array From Example | Array.prototype.from()

Javascript array from() is an inbuilt function that creates a new, shallow-copied array instance from an array-like object or iterable object.

The Array .from() lets you develop Arrays from the array-like objects (objects with a length property and indexed items) or  iterable objects ( objects where you can get its items, such as Map and  Set).

The Array from() function was introduced in ECMAScript 2015.

Javascript Array From Example

Array.from() method in Javascript is used to creates a new  array instance from a given array. If you pass a  string to the Array.from() function, then, in that case, every alphabet of the string is converted to an element of the new array instance. In the case of integer values, a new array instance simply takes the elements of the given Array.

The syntax of the Array.from() method is the following.

Syntax

Array.from(arrayLike[, mapFn[, thisArg]])

#javascript #ecmascript #javascript array from #array.prototype.from