69 lines
1.9 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
The ``++delete++`` operator can be used to remove a property from any object. Arrays are objects, so the ``++delete++`` operator can be used here too, but if it is, a hole will be left in the array because the indexes/keys won't be shifted to reflect the deletion.
The proper method for removing an element at a certain index would be:
* ``++Array.prototype.splice++`` - add/remove elements from the array
* ``++Array.prototype.pop++`` - add/remove elements from the end of the array
* ``++Array.prototype.shift++`` - add/remove elements from the beginning of the array
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
var myArray = ['a', 'b', 'c', 'd'];
delete myArray[2]; // Noncompliant. myArray => ['a', 'b', undefined, 'd']
console.log(myArray[2]); // expected value was 'd' but output is undefined
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
var myArray = ['a', 'b', 'c', 'd'];
// removes 1 element from index 2
removed = myArray.splice(2, 1); // myArray => ['a', 'b', 'd']
console.log(myArray[2]); // outputs 'd'
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this use of "delete".
'''
== Comments And Links
(visible only on this page)
=== on 28 Apr 2015, 15:33:29 Ann Campbell wrote:
back to you [~linda.martin]
You didn't have any languages targeted. I assumed JavaScript...
=== on 29 Apr 2015, 09:02:41 Linda Martin wrote:
\[~ann.campbell.2] Indeed thanks!
I updated the comment in the code snippet to show how the array look like after the deleting the element. I let you remove it or update if you think it is to heavy and not necessary.
=== on 29 Apr 2015, 09:09:47 Ann Campbell wrote:
Your updated looks good to me [~linda.martin]. I just modified it's place in the code slightly.
=== on 29 Apr 2015, 11:41:26 Linda Martin wrote:
Perfect this way! Thanks.
endif::env-github,rspecator-view[]