Angular Session-03
Angular Session-03
Session -2
Outlines
- Some Important Methods in Array
- var, let and const
www.webstackacademy.com www.webstackacademy.com
•flat()
One of the most powerful properties of JavaScript is that functions are first-class
objects.
This means that they are like any other object and have the same properties as
standard objects.
function fnCaller(fn) {
fn();
}
function log() {
console.log('Calling log');
}
The map() method is used to get a modified version of the array or a reduced value
using callback functions
map() applies a function to each array element and creates a new array of the returned
values.
thisValue: This parameter is optional. It holds the value of passed to the function.
The function that is be passed is given arguments by the map method in the
following order
Uses of Map( )
arr.reduce(<function>);
The reduce method gives arguments to the passed function in the following order:
function callbackfn( prev : any, curr : any, index: number, array: number[ ])
For each element, the callbackfn will be passed with the previous callbackfn
function’s return value as the first argument, and the value of the element as the
second argument.
If the array has only one value, that value is returned. For an empty array,
an error is thrown.
let flattened = [[0, 1], [2, 3], [4, 5]].reduce( function(accumulator, currentValue)
{
return accumulator.concat(currentValue)
}, [ ] )
Group objects by a property
let people = [
{ name: 'Matt', age: 25 },
{ name: ‘ Asma ', age: 23 },
{ name: ‘ Cami ', age: 29 }
];
arr.some(function(element) {
return element % 2 === 0; //checks to see if even
}); //true
filter()
If the element passes the test, you push that element to a new array.
var arr = [6, 4, 5, 6, 7, 7];
var x = arr . filter(function(element) {
return element/2 > 3;
})
forEach()
A method that is very similar to a for loop.
For every element found in the array, the forEach() method executes a callback
function on it.
isArray()
This method checks to see if the object passed to it is an array or not.
Returns a boolean.
Earlier before 2015, the only keyword available to declare variables was the var keyword.
Always prefer using let over var and const over let
Unchanging Values With const
Example 1
const taxRate = 0.1;
const total = 100 + (100 * taxRate);
// Skip 100 lines of code
console.log(`Your Order is ${total}`); total}`);
Hoisting variables
func();
function func() {
var a = 1;
var b = 2;
var c = 3;
console.log(a + " " + b + " " + c);
}
Thank You