[Show all top banners]

nyshangal
Replies to this thread:

More by nyshangal
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 javascript array
[VIEWED 6833 TIMES]
SAVE! for ease of future access.
Posted on 10-10-11 6:43 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

 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

 
Posted on 10-10-11 7:31 PM     [Snapshot: 23]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Dolmaji, long time! coding le bheja fry banayo jasto cha ni. 
 
Posted on 10-10-11 8:16 PM     [Snapshot: 40]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

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

 
Posted on 10-10-11 10:13 PM     [Snapshot: 85]     Reply [Subscribe]
Login in to Rate this Post:     1       ?     Liked by
 


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;
}

 
Posted on 10-11-11 12:37 AM     [Snapshot: 125]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

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
 
Posted on 10-11-11 1:36 PM     [Snapshot: 182]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

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

 
Posted on 10-11-11 7:38 PM     [Snapshot: 231]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Wow I am just comparing this to python

array=list(ab,CD,ef,gh.........)
Out=array.index(gh)

That's it
 
Posted on 10-11-11 8:20 PM     [Snapshot: 237]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

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

 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 60 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
Toilet paper or water?
ढ्याउ गर्दा दसैँको खसी गनाउच
Tourist Visa - Seeking Suggestions and Guidance
To Sajha admin
From Trump “I will revoke TPS, and deport them back to their country.”
wanna be ruled by stupid or an Idiot ?
MAGA denaturalization proposal!!
advanced parole
How to Retrieve a Copy of Domestic Violence Complaint???
and it begins - on Day 1 Trump will begin operations to deport millions of undocumented immigrants
Travel Document for TPS (approved)
All the Qatar ailines from Nepal canceled to USA
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters