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 find odd elements in an array by javascript

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

Share

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

In this article Xiaobian for you to introduce in detail "javascript how to find the odd elements in the array", the content is detailed, the steps are clear, the details are handled properly, I hope this "javascript how to find the odd elements in the array" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

Methods: 1, use the for statement to traverse the array, use the "a [I]% 2! = 0" statement in each loop to determine whether the array elements are odd, if so, output, then jump out of this cycle; 2, use the filter () method to return the elements in the array that meet the "value% 2! = 0" condition.

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

Javascript's method of finding odd numbers in an array:

Method 1: using for loop

Implementation idea: use the for statement to traverse the array, determine whether the array elements are odd in each loop, if so, output, if not, jump out of this loop.

Implementation code:

Var a = [2, 3, 4, 5, 6, 7, 8]; for (var I = 0; I < a.roomth2!) {if (a [I]% 2! = 0) {console.log (a [I]);} else {continue;}}

Method 2: using filter () method

Var a = [2 return true; 3, 4 var 5, 6, 7, 8]; function f (value) {if (value% 2! = 0) {return true;} else {return false;}} var b = a.filter (f); console.log (b)

Description:

The ilter () method returns elements in the array that meet the specified criteria.

Array.filter (function callbackfn (Value,index,array), thisValue)

Function callbackfn (Value,index,array): a callback function that cannot be omitted and can accept up to three parameters:

Value: the value of the current array element, which cannot be omitted.

Index: the numeric index of the current array elements.

Array: the array object to which the current element belongs.

The return value is a new array containing all the worthwhile true returned by the callback function. If the callback function returns false for all elements of array, the length of the new array is 0.

After reading this, the article "how to find odd elements in an array by javascript" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

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