What is an Array in jQuery with Example - onlyxcodes

Friday 26 May 2023

What is an Array in jQuery with Example

An array is a type of data structure in jQuery that enables you to store and manage a group of elements. It offers a number of ways to manipulate the items, much as arrays in other programming languages.


array in jquery with example - array list in jquery with example

An illustration of how to make and use an array in jQuery is given below:


test.html


<!DOCTYPE html>
<html>
<head>
  <title>Array Example in jQuery</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>

  <script>
  
    // Creating an array
	var country = ["usa", "canada", "australia"];

	// Accessing elements
	console.log(country[0]); // Output: "usa"

	// Modifying elements
	country[1] = "united kingdom";
	console.log(country); // Output: ["usa", "united kingdom", "australia"]

	// Adding elements
	country.push("germany");
	console.log(country); // Output: ["usa", "united kingdom", "australia", "germany"]

	// Removing elements
	country.splice(1, 2);
	console.log(country); // Output: ["usa", "germany"]

	// Iterating over elements
	$.each(country, function(index, value) {
	  console.log(value);
	});
    
  </script>
  
</body>
</html>

In the above-mentioned example, we start by creating an array called a country that has three elements: "usa," "canada," and "australia." 


Individual elements can be accessed by their index, which starts at 0. country[0] in this scenario return "usa".


By giving new values to specified indexes, we can change the values of the items. We substitute "united kingdom" for "country[1]" in the example. 


The push method can be used to add elements, and the splice method can be used to remove elements.


Utilizing the $.each function from jQuery, we can loop through the array's elements. It requires a callback function, which is run for each element and enables us to take action on them.


How to create an array list in jQuery?

Using the Array object and associated methods, you may make an array list in jQuery. 


Here is an illustration of how to make an array list in jQuery:


test.html


<!DOCTYPE html>
<html>
<head>
  <title>Array List Example in jQuery</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>

  <script>
  
	// Create an empty array
	var arrayList = [];

	// Add elements to the array
	arrayList.push("USA");
	arrayList.push("Canada");
	arrayList.push("United Arab Emirates");

	// Access elements in the array
	console.log(arrayList[0]); // Output: "USA"
	console.log(arrayList[1]); // Output: "Canada"
	console.log(arrayList[2]); // Output: "United Arab Emirates"

  </script>
  
</body>
</html>

In the above example, an empty array named arrayList is first created. 


After that, we add elements to the array using the push method. 


Finally, we may use the array's index to access specific elements.


It should be noted that since jQuery primarily focuses on DOM manipulation and utility functions, it is not necessary to use it to create an array list. 


The use of native array methods in JavaScript is seen in the example above.

No comments:

Post a Comment

Post Bottom Ad