Archive for February, 2006

Check for Duplicates in an array

This Function will check for duplicate entries in an array and return true or false

For Single Dimensional Array
Array.prototype.chkforDuplicates = function (value)
// Returns true if the passed value is found in the
// array. Returns false if it is not.

{
var i;
var ctr = 0;
for (i=0; i < this.length; i++) { // use === to check for Matches. ie., identical (===), ; if (this[i] == value) { return true; } } return false; }; For Multi Dimensional Array
Array.prototype.chkforDuplicates = function (value,Idx)
// Returns true if the passed value is found in the
// array. Returns false if it is not.

{
var i;
var ctr = 0;
for (i=0; i < this.length; i++) { // use === to check for Matches. ie., identical (===), ; if (this[i][Idx] == value) { return true; } } return false; }; ong>Note: Idx is the Value of the Column You want to check for duplicates

This js function will find the index of an array element with the given value in an array.
for Example you are having an array like

var mnuArray = new Array(“Groups”,”Services”,”Applications”);

And want to know the index/position of and element in an array, then
you can call mnuArray.findIndex(“Services”);
This will return “1”.

This will be helpful if you are having a large size array/the elements are unknown which you have populated from an dB

Array.prototype.findIndex = function(value){
var ctr = "";
for (var i=0; i < this.length; i++) { // use === to check for Matches. ie., identical (===), ; if (this[i] == value) { return i; } } return ctr; };

And advanced version of this function is,
If you want to do the same in the case of multidimensional arrays
then use the following

Array.prototype.findIndexByCol = function(value,cIdx){
var ctr = "";
for (var i=0; i < this.length; i++) { // use === to check for Matches. ie., identical (===), ; if (this[i][cIdx] == value) { //alert(this[i][cIdx]+"===="+value); return i; } } return ctr; };

Functions find Left and Right position of an Object

When a layer is inside another layer, it’s style.left and style.top positions can both be ‘0’
but the layer itself can be 500 pixels away from the actual margin. These functions will return
the true offset.
// To find the left position, add this snippet to your code:

function getPositionLeft(This){
var el = This;var pL = 0;
while(el){pL+=el.offsetLeft;el=el.offsetParent;}
return pL
}

// To find the top position, add this snippet to your code:

function getPositionTop(This){
var el = This;var pT = 0;
while(el){pT+=el.offsetTop;el=el.offsetParent;}
return pT
}

Check If an variable is an array or not in javascript

This js function will check if a variable / Object is an array or not and returns true or false

//chk if an object is an array or not.
function isArray(obj) {
//returns true is it is an array
if (obj.constructor.toString().indexOf(“Array”) == -1)
return false;
else
return true;
}

Feedback Form