1

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/

3
  • 2
    If you are matching individual names, then yes. It might be cleaner to have an array of those twenty names and then just $.inArray() or something to check in the if statement. Commented Jan 9, 2015 at 5:27
  • Do you have some identifier other than just a name? A type or anything? Commented Jan 9, 2015 at 5:30
  • what is the question? Commented Jan 9, 2015 at 5:32

3 Answers 3

2
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/

Sign up to request clarification or add additional context in comments.

Comments

0

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 .

Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.