Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an Array .I want to remove the element which is matching the Condition..



C#
for (var i = 0; i < DQuestions.length; i++)
                {
                    //for(j=0;j<rules[i].length;j++)
                    {
                        if ((DQuestions[i][0] == Id))
                        {
                            alert(DQuestions[i][1]);

                            var idx = DQuestions.indexOf(DQuestions[i]); // Find the index
              alert('idx '+idx);
              if(idx!=-1)
              {
                 DQuestions.splice(idx, 1);

                        }
                        else
                        {
                        alert('I doonno');
              }
                        }
                    }
                }
                for (i = 0; i < DQuestions.length; i++)
                    {
                       alert(DQuestions[i][0]+" AFtR"+DQuestions[i][1]);
            }






I add with the following condition

C#
for (var i = 0; i < n.length; i++)
                                {
                                alert(" LEngth "+DQuestions.length);
                                DQuestions[DQuestions.length]=new Array(Id,n[i]);
                                //  DQuestions.unshift(n[i]);
                                alert('DQuestions.length '+ DQuestions.length);
                                alert(DQuestions[i][0]+" "+DQuestions[i][1]);
                                }



It is of the type

C#
var array = new array (a,b);



I am unable to delete The value from array and out of 3 values only 2 get deleted and 2 indexed value remains undeleted
Posted
Updated 21-Aug-12 22:12pm
v2

1 solution

I might not be the most efficient way of doing it but what I do is use lists

so what i do is
C#
TypeOfArray[] myarray = new TypeOfArray[];
//load styuuf in array

List<typeofarray> toKeep = new List<typeofarray>

foreach(TypeofArray element in myArray)
{
     if(!removeConditionMet){
            toKeep.Add(element);

      }
 
}
myArray = toKeep.ToArray();



Hope this helps
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900