How to split an array using jQuery
Earlier I had posted about jQuery solution to remove item from array, combine/join arrays and Find index of element in array, And In this post, find how split an array using jQuery with example.
$(document).ready(function() { var members = [50, 10, 19, 22, 6, 74]; $('#allElements').html(members.join()); memsecond = members.splice(0,4); $('#firstPart').html(memsecond.join()); $('#secondPart').html(members.join()); });?
In this jQuery code, I have used the splice() method for splitting the array. This method requires 2 parameters. The first parameter specifies the index location from where to start splitting, and the second parameter specifies the number of elements to be removed from the original array. The range of array elements defined by the two parameters will be extracted from the original array and will be returned so it can be saved in another array.
See result below.
Read more about splice() method here.
Feel free to contact me for any help related to jQuery, I will gladly help you.