Jump to content

JavaScript Array


pankaj.ghadge

Recommended Posts

I want to add some value in array like str1,str2,str3, str4 and later on i have deleted str3 , array should be like this str1,str2, str4 So i want to know is their ant array function exists to add or delete value. like as follows.......this is just example i dont' know it is possible or not var arr1=new array();arr1.add("str1"); arr1.add("str2");arr1.add("str3");arr1.add("str4");arr1.delete("str3");is this kind of functionality available in java script. i have also searched for vector array in java script but i didn't find any thing ............so please tell me............how to do this.............

Link to comment
Share on other sites

That sort of thing is not built into Javascript, but you might try jQuery or JSON, or writing your own functionality. There are plenty of really good and free references on how to extend the functionality of Javascript.When I add items to an array I normally do as such:

var arr1 = new array();arr1[arr1.length] = "str1";arr1[arr1.length] = "str2";arr1[arr1.length] = "str3";arr1[arr1.length] = "str4";

for deleting I believe the above has covered it.

I want to add some value in array like str1,str2,str3, str4 and later on i have deleted str3 , array should be like this str1,str2, str4 So i want to know is their ant array function exists to add or delete value. like as follows.......this is just example i dont' know it is possible or not var arr1=new array();arr1.add("str1"); arr1.add("str2");arr1.add("str3");arr1.add("str4");arr1.delete("str3");is this kind of functionality available in java script. i have also searched for vector array in java script but i didn't find any thing ............so please tell me............how to do this.............

Link to comment
Share on other sites

Chris has the right idea. C-based languages (e.g., Perl, PHP, Javascript) almost all use push, pop, shift, and unshift to add/subtract values at the top and bottom of an array. Slice and splice work in the middle. There are other ways, sure, but you really have to know the foundations.

Link to comment
Share on other sites

CODE FOR DELETING ARRAY ELEMENT

<script language="javascript">// this is the original arrayvar theArray = ["pankaj","prashant","priya","trupti","tushar"];// these are the items I wish to remove from the array//var toRemove = [pankaj,priya];// it can be done with one item only as well   var toRemove = "tushar";// adding it as a prototype object enables it to be used from any arrayArray.prototype.removeItems = function(itemsToRemove) {    if (!/Array/.test(itemsToRemove.constructor))     {        itemsToRemove = [ itemsToRemove ];    }    var j;    for (var i = 0; i < itemsToRemove.length; i++)     {        j = 0;        while (j < this.length)         {            if (this[j] == itemsToRemove[i])             {                this.splice(j, 1);            }             else             {                j++;            }        }    }}theArray.removeItems(toRemove);alert(theArray);</script>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...