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