Home >Web Front-end >Front-end Q&A >jquery is not equal to the first one of 0
jQuery is one of the most popular JavaScript libraries. It provides many powerful functions that can greatly improve the efficiency and quality of our web application development. However, in actual project development, you will inevitably encounter some more difficult problems, such as finding the first element in an array or list that is not equal to 0.
Method 1: Use for loop traversal
Before performing this operation, we first need to understand the basic structure of the array or list. For example, we have the following array:
[2, 3, 4, 0, 6, 7]
What we are looking for is the first element that is not equal to 0. We You can use a for loop statement to achieve this:
var arr = [2, 3, 4, 0, 6, 7]; for (var i = 0; i < arr.length; i++) { if (arr[i] !== 0) { console.log(arr[i]); break; } }
Run the above code, the console will output 2
. The idea of this method is very simple, it is to traverse all the elements in the array, and if the current element is not equal to 0, output it and break out of the loop.
Method 2: Use ES6 method
In addition to the traditional for loop, ES6 also provides some convenient methods to traverse arrays or lists. For example, we can use the find()
method to find the first element that is not equal to 0:
var arr = [2, 3, 4, 0, 6, 7]; var firstNonZero = arr.find(function(item) { return item !== 0; }); console.log(firstNonZero);
Run the above code, the console will also output 2
. find()
The method will traverse all elements in the array in sequence, execute the callback function and pass in the current element as a parameter. If the callback function returns a value of true
, the find()
method will return the current element, otherwise it will continue to traverse the next element.
Method 3: Use jQuery methods
In addition to native JavaScript methods, jQuery also provides some convenient methods to operate arrays or lists. For example, we can use the grep()
method to find the first element that is not equal to 0:
var arr = [2, 3, 4, 0, 6, 7]; var firstNonZero = $.grep(arr, function(item) { return item !== 0; })[0]; console.log(firstNonZero);
Run the above code, the console will also output 2
. grep()
The method will traverse all elements in the array in sequence, execute the callback function and pass in the current element as a parameter. If the return value of the callback function is true
, the current element will be added to a new array, and the new array will eventually be returned. Since we only need to find the first element that is not equal to 0, we can directly retrieve the first element of the returned array.
Summary
The above three methods can all be used to find the first element in an array or list that is not equal to 0. Method one is the traditional for loop, method two is ES6’s find()
method, and method three is jQuery’s grep()
method. Just choose the method that suits you according to the actual situation.
The above is the detailed content of jquery is not equal to the first one of 0. For more information, please follow other related articles on the PHP Chinese website!