Handling Arrays using jQuery Tutorial with Examples

Today, We want to share with you Creating and Manipulating Handling Arrays.In this post we will show you , hear for How to use Arrays with Each Function in jQuery we will give you demo and example for implement.In this post, we will learn about How to Create and Manipulate Arrays in jQuery with an example.

jQuery Handling Arrays

There are the Following The simple About Handling Arrays with jQuery Full Information With Example and source code.

As I will cover this Post with live Working example to develop jQuery Creating and Manipulating Arrays, so the some major files and Directory structures for this example is following below.

  • index.html

Check if Variable is an Array

index.html

This is where I will make a simple HTML form and PHP server side source code for our web application. To make the forms simply all souce code copy and write it into your any text editor Like Notepad++, then save file it as index.html.

var manager = [];
var dev = ["Ankit", "Mayur", "Sejal"];
var boss = "Jaydeep";
console.log('manager is an array: ' + $.isArray(manager)); // Returns true
console.log('dev is an array: ' + $.isArray(dev)); // Returns true
console.log('boss is an array: ' + $.isArray(boss)); // Returns false

Find Index of an Item in Array

var devlopers = ["Ankit", "Mayur", "Sejal", "pratik"];
console.log('Index of Ankit is: ' + $.inArray("Ankit", devlopers)); //Returns 0
console.log('Index of jaydeep is: ' + $.inArray("jaydeep", devlopers)); //Returns -1
console.log('Index of Mayur is: ' + $.inArray("Mayur", devlopers, 2)); //Returns -1

Sort an Array

simple jQuery Sorting an array need to calling the main JavaScript used to sort() method on the all the array object.

var devloperName = ["Jaydeep", "Ankit", "Sejal", "pratik"];
devloperName.sort() // Will produce Ankit, Jaydeep, pratik, Sejal

Creating and Manipulating Arrays

var devloperName = ["Jaydeep", "Ankit", "Sejal", "pratik"];
devloperName.sort() // Will produce Ankit, Jaydeep, pratik, Sejal
devloperName.reverse() // Will produce Sejal, pratik, Jaydeep, Ankit
var age = [40, 70, 9, 98];
age.sort(); //wrong sort values. 40,70,9,98
age.sort(function(a, b){return a-b}); //Correct sort values. 9,40,70,98

Remove an Item from the Array

Remove an Item from the Array using jQuery


var devloperName = ["Jaydeep", "Ankit", "Sejal", "pratik"];
  var nameToRemove = "Ankit";
  devloperName.splice($.inArray(nameToRemove, devloperName), 1);

//add new devlopers to an array using the splice() method.
devloperName.splice(2,0,"Mark");

Remove Duplicate Items from an Array

simple Javascript Source code for Remove Duplicate Items from an Array

function removeDuplicate(arrItems) {
  var liveArr = [];
  $.each(arrItems, function(i, e) {
    if ($.inArray(e, liveArr) == -1) liveArr.push(e);
  });
  return liveArr;
}
var totalnum = [1, 4, 16, 10, 4, 8, 16];
var liveArr = removeDuplicate(totalnum);

Merge Two Arrays into a Single Array

In this Array case, the Data content of firstArr and thirdArr would be the simple both same. If We would nor such to alter the any data content of the my first array, after the use the following source code.

var firstArr = [10, 19, 22, 36, 50, 74, 10, 22];
var secondArr = [100,200,300];
var thirdArr = $.merge(firstArr, secondArr);
**************
var firstArr = [10, 19, 22, 36, 50, 74, 10, 22];
var secondArr = [100,200,300];
var thirdArr = $.merge($.merge([], firstArr), secondArr);
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about How to Create and Manipulate Arrays in jQuery.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment