Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report