0

Im trying to remove a entery from the array, but after the loop, it leaves an comma seperator where the removed result is.

Q: How can i remove the comma from the array?

wanted output:

after loop

[{ number: 10, count: 1 }, { number: 5, count: 1 } ]

My current code:

        console.log("before loop:");
        console.log(bNumbersCount);


            for(var key in bNumbersCount) {
                var card = bNumbersCount[key];
                if (card['count'] != x) {
                    delete bNumbersCount[key];
                }
            }


console.log("after loop");
console.log(bNumbersCount);

console log result:

before loop:
[ { number: 2, count: 3 },
  { number: 10, count: 1 },
  { number: 5, count: 1 } ]
after loop
[ , { number: 10, count: 1 }, { number: 5, count: 1 } ]
maria
  • 207
  • 5
  • 22
  • 56
  • 3
    Your code uses some variables that you didn't include in your example. You should correct this so that someone could copy your example and run it directly, without any editing on their part. – Aurora0001 Oct 16 '16 at 14:57
  • use array.splice method – sanjeevprasad Oct 16 '16 at 14:58
  • This question is repeated a lot of times [Deleting array elements in JavaScript - delete vs splice](http://stackoverflow.com/questions/500606/deleting-array-elements-in-javascript-delete-vs-splice) – Canilho Oct 16 '16 at 15:03

3 Answers3

3

This isn't how you work with arrays in JavaScript.

  1. Don't use for-in on a JavaScript array unless you know what you're doing and have a specific reason; instead, use any of the several other ways outlined in this answer.

  2. Don't use delete on an array entry unless you know what you're doing and want to create a sparse array.

You probably wanted to loop through the array and actually remove entries. Two ways to do that:

  1. Create a new array with only the entries you don't want to remove, via filter:

    aNumbersCount = aNumbersCount.filter(function(entry) {
        return card.count == x;
    });
    
  2. Use splice to modify your existing array in place, in which case you'll probably want to loop backward so the array indexes don't change on you:

    for (var n = aNumbersCount.length - 1; n >= 0; --n) {
        if (aNumbersCount[n].count != x) {
            aNumbersCount.splice(n, 1); // Removes one entry at index n
        }
    }
    

Side note: card['count'] can more simply be written card.count. You only need brackets notation and a string when the name of the property comes from an expression (such as getting a value from a variable) or if the name contains characters that make it an invalid IdentifierName (such as spaces).

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

Iterate backwards and use splice for deleting.

var array = [ { number: 2, count: 3 }, { number: 10, count: 1 }, { number: 5, count: 1 } ],
    i = array.length,
    x = 3;

while (i--) {
    if (array[i].count === x) {
        array.splice(i, 1);
    }
}

console.log(array);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You could use filter for that:

bNumbersCount = bNumbersCount.filter(card => card.count === x);

var bNumbersCount  = [ { number: 2, count: 3 }, 
                       { number: 10, count: 1 }, 
                       { number: 5, count: 1 } ],
    x = 1;
bNumbersCount = bNumbersCount.filter(card => card['count'] === x);
console.log(bNumbersCount);

The delete method does not shift the array elements to fill up the property (index) you deleted. You could use splice for that: that will shift the elements into place. But then you need to take care how you loop, as you can find your self skipping the value that followed just after the deleted/spliced value.

All this becomes more simple with .filter().

NB: Be aware that with .filter you actually create a new array. This means that if you had passed that array as function argument, the caller will not see the change. The function should then return that array to make it available to the caller.

trincot
  • 317,000
  • 35
  • 244
  • 286