javascript array

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

 I have an array, I have say suppose 10 key pairs in that arraay array[0].val=ab array[1].val=cd array[2].val=ef array[3].val=gh . . . . array[8].val=wx array[9].val= yz now I have two values say suppose "ij" and "kl" , I want to check if this array contains that value "IJ" and "kl", if it does, i want to return  "yz" in this case array with index 9, for(var i =0; i <array.length;i++){ if(array[i].val=="ij" && array[i].val=="kl"){ //code goes here,,but what ?  } } P.S i am using prototype

nyshangal · Oct 10, 2011 6:43 PM · 14,859 views

7 Replies

Dolmaji, long time! coding le bheja fry banayo jasto cha ni. 

Footyfan · Oct 10, 2011 7:31 PM

Dolmaji, first of all you are declaring the array in very difficult way. Dolmaji, hope this would help :-). var arraylist= new array(); array[0]=ab array[1]=cd array[2]=ef array[3]=gh . . . . array[8]=wx array[9]= yz for(var i =0; i <array.length;i++) { if(array[i].toString()=="ij" && array[i].toString()=="kl") { document.write(array[9].toString()); #will give you result of yz } If i helped you, can i get some chang ;-)? Peace Sirus_Me   Last edited: 10-Oct-11 08:18 PM Last edited: 10-Oct-11 08:19 PM

sirus_me · Oct 10, 2011 8:16 PM

if(array[i].toString()=="ij" && array[i].toString()=="kl") The conditional statement above is never going to pass. What your doing here is that you are comparing the same element of array i.e. array[i] to both "ij" and "kl" and excepting it to be equal to both of them at the same time using the operator &&. Also, since Javascript is case-sensitive, the array declaration should look as follows - var array = new Array(); But, that's a deprecated way of declaring array. The new and better way is by using Javascript array literals as follows - var array = [ ]; And the solution would be something like this - // if we an array as follows - var array = ['ab', 'cd', 'ef', 'gh', ..., 'wx', 'yz'];  // also assuming that array[9] = 'yz' // we want to see if it contains both 'ij' and 'kl' so // we have to look for both of them separate as follows var matchCount = 0; // also since you have said that you want to return 'xy' or array[9], lets use function becuase // the only place from where you can return (a value) is a function function contains() {      for(var indx=0; indx<array.length(); indx++) {           if(array[indx] == 'ij') {                matchCount++;           }           if(array[indx] == 'kl') {                matchCount++;           }      }      if(matchCount == 2) {  // if it contains both 'ij' and 'kl' then           return array[9]; // return array[9] (which is 'yz')       }       // else you can just return undefined      return undefined; }

fuckeetow · Oct 10, 2011 10:13 PM

Array.prototype.contains = function(obj) { var i = this.length; while (i--) { if (this[i] === obj) { return true; } } return false; } And now you can simply use the following: alert([1, 2, 3].contains(2)); // => true alert([1, 2, 3].contains('2')); // => false

xcopsgen · Oct 11, 2011 12:37 AM

@xcopsgen  i think he wanted the index value which is "i" in this case, instead of true/false . Nice solution btw.

birey · Oct 11, 2011 1:36 PM

Wow I am just comparing this to python array=list(ab,CD,ef,gh.........) Out=array.index(gh) That's it

shivanagar · Oct 11, 2011 7:38 PM

If it's not about the understanding of array in Javascript. You don't even need use prototype and add a property to Array which happens to be a function that checks the presence of a value in an array. You can simply make use the method "indexOf( )" which Array already has. [2, 3, 4].indexOf(3); // 1 [2, 3, 4].indexOf(1); // -1

fuckeetow · Oct 11, 2011 8:20 PM

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion

You might be interested in...

Recent Classifieds View all
Upcoming Events View all
Service Providers View all