Pawan
Pawan

Reputation: 32321

How to change logic inside for loop to match an element of an array?

I have 50 elements in a javascript array .

For 20 elements in the above javascript array , i need to do some manipulation while displaying them within for loop(adding class style dynamically)

This is my code

var stocks=['ABAN','ADANIENT','ADVANTA']; // i have 50 elemnts in array 

for(var i=0;i<stocks.length;i++)
{
   var stockname = stocks[i];

    if(stockname=='ABAN' || stockname=='ADANIENT')  // do i  need to write 20 elements inside the for loop 
    {
     console.log('print');   
    }
    else
    {

    }

}

http://jsfiddle.net/k34dbefs/1/

Upvotes: 1

Views: 34

Answers (3)

user3998237
user3998237

Reputation:

for(var i=0; i < stocks.length; i++) {
   var stocks2 = stocks[i];

    if ($.inArray(stocks2, stocks) > -1) {
     console.log('print');   
    }   
}

Upvotes: 0

user2478240
user2478240

Reputation: 369

Maybe you can do it this way.

var stocks=[
        {name:'ABAN',check:'1'},
        {name:'ADANIENT',check:'1'},
        {name:'ADVANTA',check:'0'}
        ]; 

for(var i=0;i<stocks.length;i++)
{
  var check = stocks[i].check;

   if(check=='1')
   {
      alert(stocks[i].name);
   }
   else
   {

   }  
}

put 1 for the check value of the first 20 names .

Upvotes: 0

Sadikhasan
Sadikhasan

Reputation: 18600

for(var i=0;i<stocks.length;i++)
{
   var stockname = stocks[i];

    if(jQuery.inArray(stockname, stocks) > -1)
    {
     console.log('print');   
    }   
}

http://jsfiddle.net/k34dbefs/9/

Upvotes: 2

Related Questions