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

Why not use for in loops to traverse arrays in vue

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces why vue do not use the for in loop to traverse the array, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Do not use for in loops to traverse the array

You should all know that the for in loop is used to traverse objects, but can it be used to traverse arrays? The answer is yes, because an array is also an object in a sense, but there are some pitfalls if you use it to traverse the array: it traverses the properties on the array prototype chain.

Let arr = [1,2]; for (let key in arr) {console.log (ARR [key]); / will print 1,2} / / but if you add a method Array.prototype.test = function () {}; for (let key in arr) {console.log (ARR [key]) to the Array prototype chain, 1, 2, ARR () {} will be printed at this time

Because we cannot guarantee that the array prototype chain will not be manipulated in the project code, nor can we guarantee that the introduced third-party libraries will not operate on it, do not use the for in loop to traverse the array.

Thank you for reading this article carefully. I hope the article "Why not use for in Loop to traverse arrays in vue" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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