77 lines
3.4 KiB
Plaintext
77 lines
3.4 KiB
Plaintext
== Why is this an issue?
|
|
|
|
The ``++delete++`` operator can be used to remove a property from any object. Arrays are objects, so the ``++delete++`` operator can be used on them too.
|
|
|
|
When you delete an element from an array using the ``++delete++`` keyword, it will remove the value but still leave behind an empty slot at that index. Therefore, a hole will be created in the array because the indexes won't be shifted to reflect the deletion. This means that the array will still have that index, but the value will be ``++undefined++``.
|
|
|
|
Arrays that have gaps or missing indexes between elements are known as sparse arrays.
|
|
|
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
let 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
|
|
----
|
|
|
|
The proper method for removing an element from an array should be one of the following:
|
|
|
|
* ``++Array.prototype.splice()++`` - removes element(s) from an array at certain indexe(s)
|
|
* ``++Array.prototype.pop()++`` - removes the last element from an array
|
|
* ``++Array.prototype.shift()++`` - removes the first element from an array
|
|
|
|
Note that these methods mutate arrays in-place. Alternatively, you could create new arrays using copying methods and exclude the element you want to remove.
|
|
|
|
[source,javascript,diff-id=1,diff-type=compliant]
|
|
----
|
|
let 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'
|
|
----
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete[delete operator]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections#sparse_arrays[Sparse arrays]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete#deleting_array_elements[Deleting array elements]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#copying_methods_and_mutating_methods[Copying methods and mutating methods]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop[Array.prototype.pop()]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift[Array.prototype.shift()]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice[Array.prototype.splice()]
|
|
|
|
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[]
|