13

3 hours ago, I asked a question in SO , about deleting a part of an object, so I linked this question to it:

delete a part of object in javascript

but now another problem occurred when I deleted from that array. I use that object to populate a FlexiGrid. but when I delete an item from that object by following code, instead of delete that item , it sets to undefined :( and flexigrid did not accept it for input data.

for (var i = 0; i < Roomdata.length; i++) {

    if(Roomdata[i].id = X) {

        delete Roomdata[i];
        break;

    }
}                

For example, imagine I have 3 items in Roomdata like this :

{item1, item2, item3}

When I call this code to delete item2 , Roomdata object looks like this :

{item1, undefined, item3}

and this is a bad format to be accepted by flexigrid as input data

Is there any solution ?

Thanks every body and sorry about my bad syntax (I am new in English)

regards , Foroughi

Ali Foroughi
  • 4,540
  • 7
  • 42
  • 66
  • possible duplicate of [JavaScript Array Delete Elements](http://stackoverflow.com/questions/500606/javascript-array-delete-elements) -- please use the search before you ask a question. – Felix Kling Feb 20 '12 at 14:05
  • The accepted answer to your previous question already suggest to use `splice`. Why do you ask this question again? – Felix Kling Feb 20 '12 at 14:07
  • Sorry @Felix , but I think because I asked a question about this issue already , I asked this question to help readers to read more understandable question ,anyway sorry about that – Ali Foroughi Feb 20 '12 at 14:08
  • @FelixKling , because i use that reply , but it not work , but i use his idea to solve my problem, so i accepted it not because his answer is correct , because his answer give me an idea – Ali Foroughi Feb 20 '12 at 14:10
  • @Rob: The duplicate answer shows how to delete an element form an array which was the question. I don't think we have to spoon feed every solution in detail. See also my comment to your answer. – Felix Kling Feb 20 '12 at 14:33

4 Answers4

58

Walk through the array in reverse order, and use .splice to remove the element.
You have to walk in the reverse order, because otherwise you end up skipping elements See below.

for (var i = Roomdata.length-1; i >= 0; i--) {
    if (Roomdata[i].id == X) {
        Roomdata.splice(i, 1);
        break;
    }
}

What happens if you don't walk in the reverse order:

// This happens in a for(;;) loop:
// Variable init:
var array = [1, 2, 3];
var i = 0;

array.splice(i, 1); // array = [2, 3]   array.length = 2
// i < 2, so continue
i++;  // i = 1    

array.splice(i, 1); // i=1, so removes item at place 1: array = [2]
// i < 1 is false, so stop.

// array = [2]. You have skipped one element.
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 1
    thank you for complete reply and its comments, anyway you help me and you are great,thanks again – Ali Foroughi Feb 20 '12 at 14:31
  • 2
    You don't have to traverse the element in reverse order as you are terminating the loop after you call `splice` anyway. If you want to remove multiple elements from an array, then yes, but in this case it is not needed. – Felix Kling Feb 20 '12 at 14:33
  • @FelixKling You're right. However, since the accepted answer at the other question didn't work, I assume that `break;` have to be removed. – Rob W Feb 20 '12 at 14:37
  • @RobW , yes , it is because I want to remove just one item and the breke the loop, i can traverse it normaly :) – Ali Foroughi Feb 20 '12 at 14:53
  • @AliForoughi Just FYI, [the accepted answer](http://stackoverflow.com/a/9359866/938089?how-to-remove-a-part-of-an-object-in-javascript) does not work, because `i < j` is always true, *or* always false. It should be `i--` instead of `j--`. – Rob W Feb 20 '12 at 15:03
  • 2
    Should that be `if (Roomdata[i].id === X) {` instead (equality vs. assignment)? – Eric Smith Apr 07 '12 at 23:54
  • Doing tests I discovered that If I get the `length` of array and use in second ´splice()` and without `for`reverse. parameter it also works. Like: `array.splice(i, array.length). But I believe this isn't the best way to do it, cus you must use the array length in each for loop. So, if your array has hundreds or thousands of elements the performance will be low, but I thinked interesting to know this another way. Thanks for your reply @RobW, helped me a lot! – Zkk May 16 '18 at 14:07
  • You don't have to loop in reverse order. See my answer, where you simply decrement the loop counter so you don't skip elements after each removal. – David R Tribble Aug 09 '22 at 01:45
4

What you have is an Array. You should use the splice() method to remove an element from an array, not by deleteing the element.

for (var i = 0; i < Roomdata.length; i++) {

    if(Roomdata[i].id = X) {

        Roomdata.splice(i, 1);
        break;

    }
}  
Matt
  • 74,352
  • 26
  • 153
  • 180
  • 2
    The second argument specifies how many items have to be removed. **`.splice(i,0)` doesn't remove anything**. Even if you replace it with `.splice(i,1)`, you're skipping elements by not accounting for the changed index. – Rob W Feb 20 '12 at 14:11
  • @RobW: Whoops. And as for skipping elements, he's breaking on removal anyway, so nothings going to get skipped. – Matt Feb 20 '12 at 14:41
3

Using splice in spite of delete.

 Roomdata.splice(i, 0);

splice attribute removes blank string elements, undefined references, NULLs and FALSEs.

it will solve your problem

Kunal Vashist
  • 2,380
  • 6
  • 30
  • 59
1

To remove all of the elements of an array matching a particular value, you can do this:

// Remove all elements in arr[] matching val
for (let i = 0;  i < arr.length;  i++) {
    if (arr[i] === val) {
        arr.splice(i--, 1);   // Remove arr[i] and adjust the loop index
    }
}

Note that this is a forward scan of the array. The decrement of the loop index is necessary so that the loop does not skip the next element after an element is removed.

David R Tribble
  • 11,918
  • 5
  • 42
  • 52