How to implement JavaScript without using delete to delete items in an array
Editor to share with you how JavaScript does not use delete to delete items in an array. I hope you will get something after reading this article. Let's discuss it together.
Do not use delete to delete items in an array.
Use splice instead of delete to delete an item in the array. Using delete simply replaces the original item with undefined, not really removes it from the array.
Do not use this method:
Var items = [12,548,'a', 2, 5478, 'foo', 8852,' Doe', 2154]; items.length; / / return 11delete items [3]; / / return trueitems.length; / / return 11 items will be equal to * items will be equal to [12,548, "a", undefined × 1, "foo", 8852, undefined × 1, "Doe", 2154] * /
Instead of:
Var items = [12,548,'a', 2, 5478, 'foo', 8852,' Doe', 2154]; items.length; / / return 11items.splice (3 Magazine 1); items.length; / / return 10 items will be equal to [12,548, "a", 5478, "foo", 8852, undefined × 1, "Doe", 2154] * /
The delete method should be used to delete a property of an object.
After reading this article, I believe you have a certain understanding of "how to implement JavaScript without using delete to delete items in an array". If you want to know more about it, please follow the industry information channel and thank you for your reading!