Quickupdate

  • Home
  • Top Trending
    • Top Android Apps
    • Top Ios Apps
  • Featured
  • About Us
  • Contact us
  • Privacy Policy and Disclaimer
Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Friday, 6 August 2021

Map Filter & Reduce in Javascript

 Developers     August 06, 2021     Javascript     No comments   

  1. Map, Filter, Reduce are the higher-order function in javascript. 

Map

  1. The map function is used to transform an array.
  2. Suppose you have this array

const arr = [5, 1, 3, 2, 6]

    3. Its possible transformations could be doubling, tripling, or finding a Binary number of each element present in the array. and it will store the transformation into the new array.

    4. 
function double(x) {
    return x * 2;
}

/*
* function map accepts function `double`.
* so the map is Higher Order function
*/
const output = arr.map(double); 
console.log(output);


    5. You can also write the above function like this

const output = arr.map((x) => x * 2);
console.log(output);


Map Example


const users = [
    { firstName: "Rahul", lastName: "More", age: 22 },
    { firstName: "Yogesh", lastName: "More", age: 20 },
    { firstName: "Rohit", lastName: "Bhosale", age: 17 },
    { firstName: "Nityanand", lastName: "Yevte", age: 28 },
    { firstName: "Sachin", lastName: "Yevte", age: 26 },
];

// get Full name of users
const fullName = users.map((x) => { // Now x has access to every elemet of array.
    console.log(x.firstName + " " + x.lastName)
});


Filter()

  1. The filter function is used to filter values present inside the array.
  2. Let's say if you want to filter out al the values which are odd, or greater than 3 inside it, or divisible by 3.
  3. Used to filter the data based on condition.
function isOdd(x) {
    return x % 2;
}
const FilterData = arr.filter(isOdd)
console.log(FilterData);


function even(x) {
    return x % 2 == 0
}

const FilterData = arr.filter(even)

function even(x) {
    return x % 2 == 0
}

const FilterData = arr.filter(even)

function greaterThan(x) {
    return x > 4;
}
const FilterData = arr.filter(greaterThan)



Filter Example


// Get the name of the users Whose age is greater than 20

const ageOfx = users.filter((x) => {
    if (x.age > 20) {
        console.log(x.firstName);
    }
})

Reduce()


  1. As the name suggests Reduce doesn't Reduce anything :D
  2. Reduce can be used to iterate over each & every element of an array & find the sum of all the elements in the array. or the largest or the max element inside the array.
  3. Instead of directly going to write Reduce Method we will just check the above (p2) example in a Non-Functional way.

function findSum(arr) {
    let sum = 0;
    for (var i = 0; i < arr.length; i++) {
        sum = sum + arr[i];
    }
    return sum
}
console.log(findSum(arr));

    4. So to find we do something like this in a non-functional way.

    Let's Do it in a Functional Way.
    

const res = arr.reduce(function (acc, curr) { // Key, Value

});


    5. So first function `reduce` accepts 2 parameters 1st is a function & we will talk later about 2nd function.

    6. initially `acc` in 0 & `curr` contains the elements of the array.
    7. if we console acc initially it will give 0 & later it will return `undefined.

    8. 

const res = arr.reduce(function (acc, curr) { // Key, Value
    acc = acc + curr;
    return acc;
});
console.log(res);

    9. & if we talk about 2nd argument it takes the initial value of acc. ( by default it is 0 )

    10. Another example using reduce. Find max element using Reduce. 

const maxUsingReduce = arr.reduce(function (max, curr) {
    if (curr > max) {
        max = curr;
    }
    return max;
});
console.log(maxUsingReduce);


If my curr element is greater than the max element. then my max element is the current element.


Reduce Example

// get The age & show the count of number of people present in that array! 

const countAge = users.reduce(function (acc, curr) {// intialValue, currentValue
    if (acc[curr.age]) {
        acc[curr.age] = ++acc[curr.age];
    } else {
        acc[curr.age] = 1
    }
    return acc;
}, {});
console.log(countAge)


   We can also use Chaining on these functions.


// Get the name of the users Whose age is greater than 20

const ageOfx = users.filter((x) => x.age > 20).map(x => x.firstName);
console.log(ageOfx);




Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Saturday, 31 July 2021

Higher-Order Functions. What is Higher-Order Functions?

 Developers     July 31, 2021     Javascript     No comments   

  1. A function that takes another function as an argument or returns a function is called a Higher-Order Function & the argument function is known as a callback function.

function x() {
    console.log('I am from function x');
}
function y(x) {
    console.log('I am from function Y')
}


  • Here function y is a Higher Order function. & function y became a callback function
  • map(), Filter(), reduce() Are the most common Higher-Order Functions in Javascript


Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Friday, 30 July 2021

JavaScript & React functions / Hooks

 Developers     July 30, 2021     Javascript, React     No comments   

Very Important Javascript functions. You must know these functions in order to call yourself a JS Developer.


  1. JSON.parse
  2. Stringify
  3. Convert Object to an array
  4. Object Keys
  5. Object Values
  6. Object Assign
  7. Array map, reduce, indexOf
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Wednesday, 30 June 2021

Few Notes on LocalStorage

 Developers     June 30, 2021     Javascript     No comments   

 Ref: https://www.w3schools.com/jsref/prop_win_localstorage.asp


  1. localStorage is the browser's database. The data is stored inside your browser in your computer's memory

  2. localStorage is specific to an origin. In other words, the localStorage for one website cannot be accessed by another.

  3. The localStorage and sessionStorage properties allow to save key/value pairs in a web browser.

  4. The localStorage object stores data with no expiration date. The data will not be deleted when the browser is closed and will be available the next day, week, or year.

  5. The localStorage property is read-only.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Sunday, 27 June 2021

Async/Await in Javascript explained like a KID

 Developers     June 27, 2021     Javascript     No comments   

 Whenever we Write async in function, that particular function will return a promise.

And when we write `await` in Function it means, it  (await) saying to request that, I am doing my work here and it will take some time, till then you can go back and complete your other task in the file/outside that function typically, 


So once the request completes all the tasks outside the function then the request will again go back to the last `await` operator and again ask that `await operator` if he completed his task or not. if he still not completed that task then `Await operator` says no please complete your all task then the request says that I completed my all the task. so await says please wait I will complete my task so you can proceed further.


so once the await completes him all task then the request will be passed further. 


if there is any other `await operator` in the function then again request asks to await operator if req can go further if he can then the next code will be executed or req. will wait for `Await operator to complete his task. once done then req can go ahead and complete the next task.







REF: https://www.youtube.com/watch?v=AyJq1RRaY_k

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Saturday, 26 June 2021

What is JWT? Json Web Token

 Developers     June 26, 2021     Javascript     No comments   

Ref: https://youtu.be/7Q17ubqLfaM

  1. What is JWT
  2. Why should you use JWT?
  3. How JWT wORKS?


Authentication: You take the username & password from the user and authenticate that particular user on your website.

Authorization: Authorization means making sure that the user sents a req to the server is the same user that actually logged in at authentication of the process.

We normally do this using the session.




                                                        How session work images above

Session gets stored in the server memory. but in JWT all the users' info present in that token only has been encoded with a secret key. JWT is not stored on a server-side.

Why JWT Over Session?

Example 1: 

let take an example of an HDFC Bank. you logged in to your net banking on the HDFC portal. now you want to pay your electricity bill for that HDFC redirects you to the different websites of HDFC that may be hosted on different servers.

so here is the key. if you use a session you are only logged in on that particular server, not on a bill pay website. so the user has to log in again on the bill pay website. that's the drawback of a session.

But, if you use JWT here, WE Store JWT token (user information) on the client side.
so we can share that JWT token to the bill pay website so the user doesn't have to go through the login process again because we are sharing the Jwt token.





Example2:

Let's say the bank s very large & they have a lot of users. so for load balancing, they may use 2 servers or more. so if there SERVER A gets too busy user may be moved to SERVER B. 

IN this case, JWT is very important. so the users don't have to login again if the server change due to load.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Older Posts Home

Popular Posts

  • How to upload the existing folder on GitHub Or GitLab?
    These are the steps to upload the existing folder on GitHub Or GitLab. Whenever you want to push your existing folder to git or GitHub you m...
  • Map Filter & Reduce in Javascript
    Map, Filter, Reduce are the higher-order function in javascript.  Map The map function is used to transform an array. Suppose you have this ...

Categories

  • FAANG (2)
  • Javascript (6)
  • Node (1)
  • Project Management (1)
  • React (9)
  • SQL (1)
  • Testing (1)

Blog Archive

  • January 2023 (1)
  • January 2022 (1)
  • November 2021 (3)
  • October 2021 (3)
  • August 2021 (1)
  • July 2021 (7)
  • June 2021 (12)
  • February 2021 (1)
  • January 2021 (1)
  • January 2020 (3)
  • August 2019 (3)