How to use array with jQuery ?
Last Updated :
20 Dec, 2023
An array is a linear data structure. In JavaScript, arrays are mutable lists with a few built-in methods, we can define arrays using the array literal. The arrays can be used in the same way in jQuery as used in JavaScript.
Syntax And Declaration:
let arr1=[];
let arr2=[1,2,3];
let arr2=["India","usa","uk"];
NOTE: The type of an array in JavaScript and all its frameworks or libraries will always be an object.
Javascript
const jQueryArr = [1, 2, 3, "GFG" , "jQuery" ];
console.log(jQueryArr, typeof jQueryArr);
|
Output
[ 1, 2, 3, 'GFG', 'jQuery' ] object
Iterating through an array in jQuery
jQuery provides a generic .each() method to iterate over elements of arrays, as well as properties of objects. In the case of an array, the callback is passed an array index and a corresponding array value each time. The method returns its first argument, the object that was iterated.
Example: The below example will explain how you can iterate through an array in jQuery using .each() method.
Javascript
const arr = [ "hello" , "from" , "GFG" ];
console.log( "traversing in array using jQuery" );
jquery.each(arr, function (index,value) {
console.log( 'index: ' + index + ' ' + 'value: ' + value);
});
|
Output:
index: 0 value: hello
index: 1 value: from
index: 2 value: GFG
Share your thoughts in the comments
Please Login to comment...