Monday 7 November 2022

Javascript: Use of .map(), .reduce(), and .filter()

.map()

Say you  receive an array containing multiple objects – each one representing a person. The thing you really need in the end, though, is an array containing only the id of each person.
There are multiple ways to achieve this. You might want to do it by creating an empty array, then using .forEach , .for(...of) or a simple .for()  to meet your goal.

// What you have
const teachers = [
{ id: 3, name: 'Alex' },
{ id: 34, name: 'Jhon' },
{ id: 23, name: 'Oggy' },
{ id: 87, name: 'Tom' }
];
// What you need [3, 34, 23, 87]


If we use .forEach()

const Ids = [];

teachers.forEach(function (teacher) {
Ids.push(teacher.id);
});

See how you have to create an empty array beforehand? 
Let’s see what it looks like when using .map()

If we use .map()

const teachersIds = teachers.map(teacher => teacher.id);

See how we don't need to initialise empty Ids array and also the code is more concise now.

Syntax is array.map(function(currentValue, index, arr), thisValue)

Parameters

ParameterDescription
function()Required.
A function to be run for each array element.
currentValueRequired.
The value of the current element.
indexOptional.
The index of the current element.
arrOptional.
The array of the current element.
thisValueOptional.
Default value undefined.
A value passed to the function to be used as its this value.

Keep in mind that the resulting array will always be the same length as the original array.




.reduce()

Just like .map(), .reduce() also runs a callback for each element of an array. What’s different here is that reduce passes the result of this callback (the accumulator) from one array element to the other.

The accumulator can be pretty much anything (integer, string, object, etc.) and must be instantiated or passed when calling .reduce().

let teachers = [{ id: 10, name: "Tom", years: 14 },
{ id: 2, name: "Wexley", years: 30 },
{ id: 41, name: "Lintra", years: 16 },
{ id: 99, name: "Asty", years: 22 }];

 With .reduce(), it’s pretty straightforward:

const totalYears = teachers.reduce((acc, teacher) => acc + teacher.years, 0);

I’ve set the starting value as 0. I could have also used an existing variable if necessary. After running the callback for each element of the array, reduce will return the final value of our accumulator


If I  say I want to find which pilot is the most experienced one of all, I can use reduce as well:

I named my accumulator oldest. My callback compares the accumulator to each teacher. If a teacher has more years of experience than oldest, then that teacher becomes the new oldest so that’s the one I return


const mostExpTeacher = teachers.reduce(function (oldest, teacher) {
return (teacher.years || 0) > teacher.years ? oldest : teacher;
}, {});

As you can see, using .reduce() is an easy way to generate a single value or object from an array.

Syntax is   array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

Parameters

ParameterDescription
function()Required.
A function to be run for each element in the array.
Reducer function parameters:
totalRequired.
The initialValue, or the previously returned value of the function.
currentValueRequired.
The value of the current element.
currentIndexOptional.
The index of the current element.
arrOptional.
The array the current element belongs to.
initialValueOptional.
A value to be passed to the function as the initial value.

The accumulated result from the last call of the callback function.



.filter()

What if you have an array, but only want some of the elements in it? That’s where .filter() comes in! Here’s our data:

const pilots = [{id: 2, name: "Wedge Antilles", faction: "Rebels"},
{id: 8, name: "Ciena Ree", faction: "Empire"},
{id: 40, name: "Iden Versio", faction: "Empire"},
{id: 66, name: "Thane Kyrell", faction: "Rebels"}];

Say we want two arrays now: one for rebel pilots, the other one for imperials. With .filter() it couldn’t be easier!

const rebels = pilots.filter(pilot => pilot.faction === "Rebels");
const empire = pilots.filter(pilot => pilot.faction === "Empire");

Basically, if the callback function returns true, the current element will be in the resulting array. If it returns false, it won’t be.Returns array containing the elements that pass the test. If no elements pass the test it returns an empty array.

Syntax is array.filter(function(currentValue, index, arr), thisValue)


Parameters

ParameterDescription
function()Required.
A function to run for each array element.
currentValueRequired.
The value of the current element.
indexOptional.
The index of the current element.
arrOptional.
The array of the current element.
thisValueOptional. Default undefined
A value passed to the function as its this value.






Friday 24 March 2017

How to send a fake email?

How to Send Anonymous Emails to Anyone


There are a lot of guys out there who always keep on searching something different and with those things they either gain knowledge or prank with their friends. Sometimes they hurt other people too by doing such kind of things like when they send Anonymous Emails or SMS then it really annoying and hurting at times.


 I think so many of you have already tried this or aware about it but I want to share it for those who are still not aware of it.

There is not any special tips I am going to share you, rather I just want to introduce you with a website from which you can send emails anonymously to anyone. Lets try it step by step:-
Go the website emkei.cz
You will see the interface as:-

Fill the required blocks
From Name  any fake name(say “optimus”)
From E-mail  any fake email(say “optimus777@mymail.com”)
To Fill the correct email you want to send the email.
Subject
fill anything you want.
Attachment: 
attach anything you want.

Content type: Select anything you want.
Text: 
Write the email body.
Fill  Captcha and click on Send.
A point to be noted that there are alot of more sites that you can use to make an anonymous mail.Some are-
Tormail
Secure mail
GuerrillaMail
Cyber Atlantis


That’s all, You are done!

Saturday 12 November 2016

The 8 Important URLs That Every Google User Should Know

Which websites and apps have access to your Google account? Where do they store your searches and location history? These 8 links will reveal everything Google knows about you.

Sunday 16 October 2016

The Best Way to Secure Your Google Email Account






GETTING STATRED


Hello, welcome to my first blog about  google account security.
It is very essential to secure your Google Gmail account and a password is simply not enough now. Have you ever wondered how much we depend on Google services for every online activity, and not only Gmail, and all this is secured by a single password. With password hacking too easy nowadays, what is the next level of password security?



2 Step Verification

Welcome to 2 Step authentication. Frankly speaking this feature has been around for a long time and many people who had their email hacked will tell you they wished they had activated  2 Step authentication,  but how many actually do it?
It basically means that when you login, Google will send the user a one time password (OTP) on their mobile phone (to confirm it is indeed you!) and once you enter the verification code, you can login in. It seems difficult to do this everytime, but read on …



Setup 2 Step Authentication

Go to your Google account and login – https://myaccount.google.com. Then run the Google Security checkup. It will allow you to setup all your security features on your account one by one.


Alternatively you can directly visit http://accounts.google.com/SmsAuthConfig and check how your 2 step authentication is set up. Basically you need to specify a mobile phone number where you can receive OTP and enter the verification code after the password. You can also opt for voice
calls.


You can choose then choose trusted devices and computers so it will not prompt for OTP again. So this makes it real easy for users.

But my mobile is not with me? Just incase you are travelling and your mobile is lost/ not available, you can choose to print OTP codes, use a physical security key by Yubico (which I will share in the next post) and even have back up mobile phone options.

















Google Email Account Security

 How Do I use it? 

I had activated this feature many time but
 getting a One time password (OTP) on my
mobile phone at every login was tedious and
 for  the sake of  ease I deactivated it many times.  



 But now the feature is easier to implement as
 it allows trusted devices where once you set up
  2 Step authentication, it will not keep prompting
 you for OTP on your everyday computer or 
mobile Gmail app. While if any hacker tries
 to access you account he needs your mobile phone!






Its a 1 time setup – So basically it is a one time setup, and then it helps to keep your account secured forever. It takes a few minutes, but is 100% worth your time to secure your Google account. Its better than security questions which are easy to guess or you usually forget. Do it today.

For more details you can also visit 

For more updates follow my blogs;
THANK YOU!
If you found my blog useful like , share  and follow me.