开发者

How would I remove a "row" in an array depending on the value of an element?

开发者 https://www.devze.com 2023-02-19 08:18 出处:网络
Here\'s what I\'m currently doing/trying to do to accomplish my goal. But it is not removing the \"row\" the way I would like it too.

Here's what I'm currently doing/trying to do to accomplish my goal. But it is not removing the "row" the way I would like it too.

So, I'm making an object, then pushing it into an array. And the adding to the array part works fine and just as I expect.

var nearProfileInfoObj:Object = new Object();
nearProfileInfo开发者_如何学CObj.type = "userInfo";
nearProfileInfoObj.dowhat = "add";
nearProfileInfoObj.userid = netConnection.nearID;
nearProfileInfoObj.username = username_input_txt.text;
nearProfileInfoObj.sex = sex_input_txt.selectedItem.toString();
nearProfileInfoObj.age = age_input_txt.selectedItem;
nearProfileInfoObj.location = location_input_txt.text;
nearProfileInfoObj.headline = headline_input_txt.text;

theArray.push(nearProfileInfoObj);

So after that later on I need to be able to remove that object from the array, and it's not working the way I'm expecting. I want to take a variable whoLeft and capture their ID and then look in the array for that particular ID in the userid part of the object and if its there DELETE that whole "row".

I know you can do a filter with an array collection but that doesnt actually delete it. I need to delete it because I may be adding the same value again later on.

whoLeft = theiruserIDVariable;
theArray.filter(userLeaving);

public function userLeaving(element:*, index:int, arr:Array):Boolean
{

    if (element.userid == whoLeft)
        {

            return false;
        }
        else
        {
            return true;
        }
}

But this doesnt seem to be deleting the whole row like it implies. Does anyone know what i'm doing wrong?


Instead of modifying the original array, the new filtered array is returned by the filter method. So you need to assign the returned array to theArray.

Try this

theArray = theArray.filter(userLeaving);


EDIT This turned out to be slower than for loop:

An alternative to the hand coded loop could be something like this:

theArray.every(searchAndDestroy);

public function searchAndDestroy(element:*, index:int, arr:Array):Boolean
{
    if (element.userid == whoLeft)
    {
        arr.splice(index,1);
        return false;
    }
    return true;
}

As far as I know, every() terminates the first time the test function returns false. So the question is: for a big list, which is faster, the for loop or the loop that every() does with the overhead of the test function call.

EDIT #2 But this was faster than a for loop for a test I ran on an array of a million Points:

for each(var element:Object in theArray)
{
    if (element.userid==whoLeft)
    {
        theArray.splice(theArray.indexOf(element),1);
        break;
    }
}


I think this is what you're looking for:

for(var i:uint = 0, len:uint = theArray.length; i<len; i++)
{
   if(thisArray[i].id == whoLeft.id)
   {
      thisArray.splice(i, 1);
      break;
   }
}

However, do you really need it in an Array because you could always use a Dictionary which would mean accessing it by id which would be a lot simpler to remove.

0

精彩评论

暂无评论...
验证码 换一张
取 消