Javascript Tips and Tricks for JavaScript Array
Post
Cancel

Tips and Tricks for JavaScript Array

At the very first sight, I don’t like JavaScript!

And till now, I still don’t like it LOL

But because of my job, I have to deal with this it :)

In this post, I’ll show you guys some tips and tricks for JavaScript Array. Of course, I found them while wanderring on the Internet, searching for my *** problem :)

1. Do not use new Array()

Please use [] instead of the built-in constructor new Array().

The two statements below both create an empty array:

var test = new Array(); // AVOID THIS WAY
var test = []; // USE THIS WAY

The two  statements below both create an array that contains 5 numbers:

var test = new Array(5, 9, 4, 3, 2); // AVOID THIS WAY
var test = [5, 9, 4, 3, 2]; // USE THIS WAY

The reason we abandon the built-in constructor new Array() is that It makes your code complex and can cause nasty side effects.

var test = new Array(11, 6);    // Creates an array with two elements (11 and 6)
var test = new Array(11);	// Creates an array with 11 undefined elements!!!!!!

 2. Convert JavaScript Array to CSV

JavaScript provides valueOf() method to convert an array to a CSV (Comma Seperated Value) string.

var cities = ['Hanoi', 'Ho Chi Minh', 'Da Nang', 'Hai Phong'];
var str = cities.valueOf();
//print str: Hanoi,Ho Chi Minh,Da Nang,Hai Phong
If you want to change the comma to any other character like *,, -,… use join() method:
var cities = ['Hanoi', 'Ho Chi Minh', 'Da Nang', 'Hai Phong'];
var str = cities.join("|");
//print str: Hanoi|Ho Chi Minh|Da Nang|Hai Phong

 3. Remove Array Element by Index

To remove an Array Element by index, we use splice() method:

var cities = ['Hanoi', 'Ho Chi Minh', 'Da Nang', 'Hai Phong'];
//print cities: Hanoi,Ho Chi Minh,Da Nang,Hai Phong
var index = 2;
cities.splice(index, 1);
// print cities: Hanoi,Ho Chi Minh,Hai Phong

 4. Remove Array Element by Value

The below code snippet describe a function within Array class that allows you to remove an array element by an input value:

Array.prototype.removeByValue = function(val) {
    for(var i=0; i<this.length; i++) {
        if(this[i] == val) {
            this.splice(i, 1);
            break;
        }
    }
}

var cities = ['Hanoi', 'Ho Chi Minh', 'Da Nang', 'Hai Phong'];

cities.removeByValue("Hanoi");
// print cities: Ho Chi Minh,Da Nang,Hai Phong

 5. Empty a JavaScript Array

By default, JavaScript does not provide any method to empty an array.

The most common way we use to empty an arry is:

var cities = ['Hanoi', 'Ho Chi Minh', 'Da Nang', 'Hai Phong'];
cities = [];

This way can clear all data of cities array but it can lead to some reference problems!

For example:

var cities = ['Hanoi', 'Ho Chi Minh', 'Da Nang', 'Hai Phong'];
var myCities = cities;
cities = [];

//print cities: ''
//print myCities: Hanoi,Ho Chi Minh,Da Nang,Hai Phong

So, to empty an array correctly withou causing any side effects, just set the array length to 0! Simple, huh? :)

var cities = ['Hanoi', 'Ho Chi Minh', 'Da Nang', 'Hai Phong'];
var myCities = cities;
cities.length = 0;

//print cities: Hanoi,Ho Chi Minh,Da Nang,Hai Phong
//print myCities: Hanoi,Ho Chi Minh,Da Nang,Hai Phong

Done :D

If you have any tips and tricks for JavaScript Array, don’t hesitate to post a comment below! Thanks :)

This post is licensed under CC BY 4.0 by the author.