Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement JavaScript without using delete to delete items in an array

Shulou Source: shulou.com Published: 2022-06-01 05:41:36 10月01日 Update

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!

Tags: Arrays articles just done objects properties methods methods more knowledge industry information information channels channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia MySQL Microsoft Xiaomi