Wednesday, September 9, 2009

JavaScript Array List

Using JavaScript Arrays for creating Javascript Linked List - using jQuery 1.3.2 !

I tried many ways and also google for it, but i didnt get any logical answer for my requirement, many were using the traditional pointer-like based approach. Here I am using simple for loop for Traversing-forward.

About code:
Here my Object will be like ..
$valobj={
index="",
value=""
}
Functions:
put(index, value) - instead of using array index, i am using my own index to store corresponding values in the Array ($valarray).

removeval(myparam) - depending upon the param passed, i am deleting the Index.


display() - to iterate over the array !

- PLEASE FEEL FREE TO ADD COMMENTS SUGGESTIONS -

Keywords: ArrayList Javascript ArrayList Arrays List jQuery Simple List

var $valarray = [];
function put(inindex,invalue){
console.log("push with inid "+inindex+" invalue"+invalue);
var $valobj= new Object();
$valobj.index=inindex;
$valobj.value=invalue;
$valarray.push($valobj);
//console.log($valarray.length);
display();
}
function removeval(myparam){
console.log("remove this value, whose id is ->"+myparam);
$.each($valarray, function(i, value1) {
if(value1.index==myparam){
console.log("made null");
value1.value="null";
//$valarray.splice($valarray.index,1);
}
});
console.log("$valarray.lenght"+$valarray.length);
}
function display(){
console.log("valarray starts+++");
$.each($valarray, function(index, value1) {
if(value1.value!="null"){
console.log(value1.index);
console.log(value1.value);
}
});
console.log("valarray ends----");
}

NOTE: I am using FIREFOX - FIREBUG plugin for logging. You can comment that if you don't require

No comments:

Post a Comment

Happy Reading!

Ganex Improves .. Headline Animator