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 does es6 detect if there is an element in an array

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how es6 detects whether there is an element in the array". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to detect an element in an array by es6" can help you solve the problem.

Detection methods: 1, with the "arr.indexOf (" element value ")" statement, if the return element subscript exists; 2, with the "arr.includes (" value ")", if there is a return true;3, use the for loop statement to traverse the array, use the if statement and the "=" operator to determine whether the array element is the specified value.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Es6 detects whether there is an element in the array

Method 1: array.indexOf

This method determines whether a value exists in the array, and if so, returns the subscript of the array element, otherwise-1.

Var arr=; var index=arr.indexOf (3); console.log (index)

Method 2: array.includes (searcElement [, fromIndex])

This method determines whether a value exists in the array, and returns true if so, false otherwise

Var arr=; if (arr.includes (3)) console.log ("exist"); else console.log ("not exist")

Method 3: use for loop and if

Var arr= [1, 2, 3, 3, 4]; var kicking 0 for (var item0, 10)

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