Monthly Archives: December 2012

Remove duplicate elements from array?

Introduction

In this article you can view a short tips about JavaScript which is very useful in your programming life.

As I love jQuery.js so that first I explain how to remove duplicate element from Array using jQuery method. It is a single line of code.

JavaScript Array:

var arr1 = [‘Java’,’DotNet’,’PHP’,’Python’,’Ruby’,’Java’,’java’,’JavaScript’,’jQuery’];

Using jQuery:

var arr2 = jQuery.unique(arr1);
// [“jQuery”, “JavaScript”, “java”, “Ruby”, “Python”, “PHP”, “DotNet”, “Java”]

You can get both element in array [“Java”,”java”]. it just due to JavaScript is case sensitive.

Using core Javascript:

// Take a blank array;
var arr2 = [];

arr1.forEach(function(value){
if(arr2.indexOf(value) == -1){
arr2.push(value);
}
})

// Output:[“Java”, “DotNet”, “PHP”, “Python”, “Ruby”, “java”, “JavaScript”, “jQuery”]

Whole example:

var arr1 = [‘Java’,’DotNet’,’PHP’,’Python’,’Ruby’,’Java’,’java’,’JavaScript’,’jQuery’];
var arr2 = [];
arr1.forEach(function(value){
if(arr2.indexOf(value) == -1){
arr2.push(value);
}
})

Watermark your web page

As per subject it’s very funny to make your web page watermark. In order to make your web page watermark you don’t require to load any JavaScript or not require make any JavaScript code or HTML or Any other web technology code. So just think about it what I want to say about watermark web page. You just need to make three lines of CSS code to make watermark web page.

body{
background-image:url(‘watermark.gif’);
background-repeat:no-repeat;
background-position:center;
}

Click To View Demo