Home > Web Front-end > Front-end Q&A > How to traverse an array in jquery

How to traverse an array in jquery

王林
Release: 2023-05-28 09:02:07
Original
2357 people have browsed it

jquery is an excellent Javascript library that provides many convenient APIs to handle various DOM and data operations. When we need to traverse an array, jquery also provides a variety of methods to achieve it.

  1. $.each() method

$.each() method is a method used to traverse arrays in jquery. It can traverse any type of collection. Includes arrays, objects and NodeList. The specific syntax is as follows:

$.each(arr, function(index, value) {
  // 遍历操作
});
Copy after login

This method receives two parameters. The first parameter arr is the collection that needs to be traversed, and the second parameter function is the function body executed during traversal, including the two parameters index and value. Represents the index and value of the current element respectively.

For example, we want to traverse an array arr and print the value and index of each array element:

var arr = ['a', 'b', 'c'];

$.each(arr, function(index, value) {
  console.log('index: ' + index + ', value: ' + value);
});
Copy after login

The output result is:

index: 0, value: a
index: 1, value: b
index: 2, value: c
Copy after login
  1. $.map () method

$.map() method can also be used to traverse an array, but unlike the $.each() method, it can process the traversal results and return a new array . The specific syntax is as follows:

$.map(arr, function(value, index) {
  // 处理操作
  return value;
});
Copy after login

This method receives two parameters. The first parameter arr is the array to be traversed, and the second parameter function is the function body executed during traversal, including the two parameters value and index. Represents the value and index of the current element respectively.

For example, we want to traverse an array arr and convert the value of each element to uppercase letters:

var arr = ['a', 'b', 'c'];

var newArr = $.map(arr, function(value, index) {
  return value.toUpperCase();
});

console.log(newArr); // ['A', 'B', 'C']
Copy after login

Note that the $.map() method returns a new array , the original array arr will not be modified.

  1. $.grep() method

$.grep() method can filter elements that meet the conditions in the array and return a new array. The specific syntax is as follows:

$.grep(arr, function(value, index) {
  // 筛选条件
  return ...
});
Copy after login

This method receives two parameters. The first parameter arr is the array that needs to be traversed, and the second parameter function is the filtering condition, including two parameters value and index, which represent the current element respectively. value and index. If true is returned, it means that the element meets the conditions and is filtered out.

For example, we want to filter out all even numbers in an array:

var arr = [1, 2, 3, 4, 5];

var evens = $.grep(arr, function(value, index) {
  return value % 2 === 0;
});

console.log(evens); // [2, 4]
Copy after login
  1. $.inArray() method

$.inArray() method You can find the index position of an element in the array, and return -1 if it does not exist. The specific syntax is as follows:

$.inArray(value, arr);
Copy after login

This method receives two parameters. The first parameter value is the element that needs to be found, and the second parameter arr is the array that needs to be traversed.

For example, we want to find the index position of element 'c' in an array arr:

var arr = ['a', 'b', 'c'];

var index = $.inArray('c', arr);

console.log(index); // 2
Copy after login

It should be noted that the $.inArray() method returns a number, indicating that the element is in The index position in the array. If the element does not exist, returns -1.

In summary, the methods for traversing arrays in jquery include $.each(), $.map(), $.grep() and $.inArray(), etc. We can choose different methods according to our needs. to implement different operations.

The above is the detailed content of How to traverse an array in jquery. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template