jQuery.each()
functionThe jQuery.each()
or $.each()
can be used to seamlessly iterate over any collection, whether it is an object or an array. However, since the $.each()
function internally retrieves and uses the length
property of the passed array or object. So, if you it has a property called length
— e.g. {en: 'english', length: 5}
— the function might not work properly.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Print Array Values with Loop</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
var fruitsArray = ["Apple", "Banana", "Orange", "Mango", "Pineapple"];
$.each(fruitsArray, function(index, value){
$("#result").append(index + ": " + value + '<br>');
});
});
</script>
</head>
<body>
<div id="result"></div>
</body>
</html>
Similarly, you can print contents of an object through a loop, as shown below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Display Object Contents with Loop</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
var supercarObject = {"brand": "Lamborghini", "model" : "Huracan", "origin": "Italy"};
$.each(supercarObject, function(key, value){
$("#result").append(key + ": " + value + '<br>');
});
});
</script>
</head>
<body>
<div id="result"></div>
</body>
</html>
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to Replace innerHTML of a Div using jQuery
Use the jQuery html() Method You can...Image Crop and Upload using Croppie jQuery plugin
In every social networking website, ther...Laravel 8 Create Events in Fullcalendar using Ajax
In this article,I will show you how you...How to Protect Upload and Secure PDF Files in Laravel
In this article, i will apportion with y...How to Get Current Location in JavaScript Example
In this article, I will share with you h...