从数组中删除元素 [英] Removing element form the array

查看:92
本文介绍了从数组中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Array.我要删除与Condition匹配的元素..



I have an Array .I want to remove the element which is matching the Condition..



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

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

var array = new array (a,b);




我无法删除数组中的值,并且只有3个值中的2个被删除,而2个索引值仍未删除.




I am unable to delete The value from array and out of 3 values only 2 get deleted and 2 indexed value remains undeleted

推荐答案

我可能不是最有效的方法做到了,但我要做的是使用列表

所以我要做的是
I might not be the most efficient way of doing it but what I do is use lists

so what i do is
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


这篇关于从数组中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆