Javascript. How to count the number of specific elements in an array

Sometime we need to count, how many elements there are in an array of Javascript, but specific elements, for example if you have an array like this:

[1, 2, 3, 5, 2, 8, 9, 2]

Then you need to know how many elements there are of the number 2, the easy form maybe can be, if you create a cycle to count each one elements and writing a conditional with the element to compare if exists or not, look this:

var count = 0;
for(var i = 0; i < array.length; ++i){
    if(array[i] == 2)
        count++;
}

If you want beautify this, maybe you can create a method with parameters, something like this:

function countEelements(element,array)
{
var count = 0;
for(var i = 0; i < array.length; ++i){
    if(array[i] == element)
        count++;
}
return count;
}

that is better, but in Javascript exists a method called filter in the arrays.

The explanation is:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Then if you write something like this:

var count = array.filter(function(x){return x==2}).length

You will get the total of elements of an array, that is very easy.

 

How to clone an object in JavaScript

In JavaScript the assignation of the objects always is through references, for example:

var ob1 = {a:'hi'};

var ob2 = ob1;

If you print this, It show the following:

console.log(ob1);

console.log(ob2);

{ a: 'hi' }

{ a: 'hi' }

What happend if you modify ob2? For example:

ob2.a = 'hi ob2';

Now I’ll print the value of both of them

console.log(ob1);

console.log(ob2);

The result is:

{ a: 'hi ob2' }

{ a: 'hi ob2' }

The value of both is the same, because the value of ob2 is the reference to ob1,  then how we can have two objects diferents, or exactly how to clone an object?.

That is very simple, you just need convert this object to string and convert it to object again.

var ob1 = {a:'hi'};

var ob2 = JSON.parse(JSON.stringify(ob1));

With this, now you have differents objects, if you modify ob2, this change does not affect to object ob1.

Very simple.

Programing a app to send massive emails

I know the following is not an app (app mobile, app web,.. ) it is a little program to PC, but in this days I’m not sure how to say it, whatever.

In my work we needed a way to send emails from a list, but we didn’t not want to use an external service like mailchimp for example, then we need a simple way to send those emails.

The requeriments

The email’s list will be in csv format, looks like this:

email1@email.com,Jon Doe

emial2@email.com, Rick Doe

Why need two fields? because we need into the email a greeting, for example:

Hi Jon Doe

Loremp ipsum

So each email will have the name of the person.

Continue reading Programing a app to send massive emails

MongoDB. How to install mongodb on Ubuntu 16.04

Maybe you need a MongoDB server on your system linux, in this post I will be enumerate the steps for install MongoDB on ubuntu version 16.04 that at now is the LTS version.

The first step is add the public key, you need write the following command into terminal

Continue reading MongoDB. How to install mongodb on Ubuntu 16.04

Microsoft give us books about technology

I found a publication of Microsoft that make available many books about technology, some topics are: Windows 10, Office 365, Office 2016, Power BI, Azure, Windows 8.1, Office 2013, SharePoint 2016, SharePoint 2013, Dynamics CRM, PowerShell, Exchange Server, System Center, Cloud, SQL Server.

The books are in three differents format: PDF, MOBI, EPUB.

Really are many books and in my opinion are very worthwhile, maybe in a few days I can download all this books and make them available in a pack.

Publication in Spanish