≡ Menu

How to loop through or enumerate the properties of a JavaScript object?

There are multiple ways to iterate/loop through all the properties in a JavaScript object.

Method 1: for-in loop


var person = {
  name: 'Shravan Kasagoni',
  age: 20,
  isActive: true
};

person.constructor.prototype.printName = function() {
  console.log(this.name);
};

person.constructor.prototype.encKey = 'somevalue';

person.constructor.prototype.printName = function() {
  console.log(this.name);
};

The for-in loop iterates over all enumerable properties of an object.


for (var propertyName in person) {
  console.log(propertyName, person[propertyName]);
}

Result:


name Shravan Kasagoni
age 20
isActive true
printName ƒ () {
  console.log(this.name);
}
encKey somevalue

The for-in loop returns all the enumerable, own and prototype properties of an object.

In the above person object, name, age, isActive are own properties, printName, encKey are prototype properties. We can filter out the prototype properties using hasOwnProperty() method.


for (var propertyName in person) {
  if (person.hasOwnProperty(propertyName)) {
    console.log(propertyName, person[propertyName]);
  }
}

Result:


name Shravan Kasagoni
age 20
isActive true

Continue Reading

Reset iTerm2 Preferences to Default

I have been playing around settings in the iTerm2 preferences to customize according to my needs, instead of creating a new profile I have made the changes to default profile itself.

I want to reset all the changes I have made and start the fresh with default profile again.

Using below two commands, we can accomplish that.


cd ~/Library/Preferences/
defaults delete com.googlecode.iterm2

Once we execute above commands in the terminal we need to restart the iTerm application for the changes to take effect.

Download the iTerm2 from https://www.iterm2.com.

How to Install and Configure the Apache HTTP Server with SSL on Red Hat Linux

This blog post shows how to install and set up the Apache HTTP Server with SSL on Red Hat Linux (7.x), and we can follow the similar steps for other flavors of Linux.

Step 1: Install Apache HTTP Server


sudo yum install httpd

Step 2: Enable and start the httpd service


sudo systemctl enable httpd.service
sudo systemctl start httpd.service

Step 3: Enable the required ports


sudo iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT -p tcp -m tcp --dport 443 -j ACCEPT

In above the step we enabled port 80 for http and 443 for https traffic. To configure the SSL, we need to install an additional module mod_ssl.

Step 4: Install mod_ssl module


sudo yum install mod_ssl

This blog post assumes you already have a digital certificate (my-web-site.crt) and private key (my-web-site-private.key).

Continue Reading

Delete all lines in VI editor

Follow the below steps to delete all the lines in a file (emptying file) using VI / VIM editor.

  1. Open the file vi <file-name>
  2. Press gg, this will move the cursor to the first line of the file.
  3. Now press dG, this deletes all the lines.
  4. Finally, type :wq to save the changes and exit from the file.

Happy Coding!!

Update the local list of remote branches in the git repository

Below command shows, all the git branches (local, remote) in the local git repository.


git branch -a

The remote branches list the local git repository won’t be updated automatically even someone removes the remote branch on the server.

We can use the below command to update the local list of remote git branches.


git remote update origin --prune

Instead of the above command, we can use the flag --prune with git fetch or git pull to update the local list of remote git branches every time.


git fetch --prune

git pull --prune

We can set to update the local list of remote git branches automatically every time we run git pull or git fetch using below command.


git config remote.origin.prune true

For the above command to work, we must be using git version 1.8.5 or above.