JS Part3
1. filter (concise):
const number = [1,2,3,3,4,4,5,6,7,8,9]
const unique = number.filter((item,index)=>(number.indexOf(item)= = = index) );
filter will always return new array. It will not modify exixting array. isBiggerThanFive is the inline callback function
number.filter(isBiggerThanFive);
function isBiggerThanFive(n){
return n>5;
}
In callback function we can test the conditions and return the callback.
2. map (ES6 --includes)
We can use any type of key and values
const myMap = new Map() // empty map object
console.log(myMap)
const key1 = 'mystr'
const key2 = {}
const key3 = function(){}
string map values
myMap.set(key1, 'this is a string');
myMap.set(key2, 'This is a blank object');
myMap.set(key3, 'this is empty function')
console.log(myMap);
// getting a values from map
let value1 = myMap.get(key1);
console.log(value1);
//get the size of the map
console.log(myMap.size);
you can loop using for of to get keys and values
for(let[key,value] of myMap){
console.log(key,value)
}
get only keys
for (let key of myMap.keys()){
console.log(key);
console.log('key is', key);
}
// get only values
for(let values of myMap.values()){
console.log('value is', value);
}
// you can loop through a map using for each loop
myMap.forEach((v,k)=>{
console.log('key is',k);
console.log('value is',v)
)}
// convert map to an array
let myArray = Array.from(myMap);
console.log('map to array is ', myArray)
// converting mapkey to an array
let myKeyArray = array.from(myMap.key())
console.log('map to array is', mykeyArray);
converting map values to an array
let myValuesArray= Array.from(myMap.values);
console.log('map to array is', myValuesArray);
merge Map-- flattering operator
observable ---- (subscrive =>(res))
(subscribe => (res)=>res.data)
source.subscribe(rs=>{console.log(rs)})
source.subscribe(res=>res.subscribe(res2=>{console.log(res)}))
array to observable we use from operator
const source = ['tech', 'comedy', 'news']
const source= from(source)
source.subscribe((res:any)=>{
console.log(res);
}
of() used to create observable
const source = of(['tech','comdey', 'news'])
MergeMap is the combination of map & mergeAll
Comments
Post a Comment