Home >Web Front-end >JS Tutorial >Implementation code for using jQuery to operate object array_jquery

Implementation code for using jQuery to operate object array_jquery

WBOY
WBOYOriginal
2016-05-16 18:07:19945browse

jQuery mainly provides the following tools for array element operations:
(1) Iteration of arrays and objects: jQuery.each(obj,callback)
General iteration method, which can be used to iterate objects and arrays. The callback function has two parameters: the first is the member of the object or the index of the array, and the second is the corresponding variable or content. If you need to exit the each loop, you can make the callback function return false, and other return values ​​will be ignored.
(2) Filtering of array elements: jQuery.grep(array,callback,[invert])
Use the filter function to filter array elements. This function passes at least two parameters: the array to be filtered and the filter function. The filter function must return true to retain the element or false to remove the element.
(3) Search for array elements: jQuery.inArray(value,array)
Determine the position of the first parameter in the array (return -1 if not found).
(4) Delete duplicate elements: jQuery.unique(array)
Delete duplicate elements in the array.
The following example uses filtering of object arrays to obtain or delete array elements whose specified attributes are specified values.

Copy code The code is as follows:


<script> <br>/**<br>* Delete the object whose property is objPropery and whose value is objValue element from the object array<br>* @param Array arrPerson array object<br>* @param String objPropery object's property<br>* @param String objPropery object's Value<br>* @return Array filtered array<br>*/ <br>function remove(arrPerson,objPropery,objValue) <br>{ <br>return $.grep(arrPerson , function(cur,i){ <br>return cur[objPropery]!=objValue; <br>}); <br>} <br>/**<br>* Get the object whose property is objPropery and whose value is objValue element from the object array<br>* @param Array arrPerson array object<br>* @param String objPropery object’s attribute<br>* @param String objPropery object’s Value<br>* @return Array filtered array<br>*/ <br>function get(arrPerson,objPropery ,objValue) <br>{ <br>return $.grep(arrPerson, function(cur,i){ <br>return cur[objPropery]==objValue; <br>}); <br>} <br>/ **<br>* Display object array information <br>* @param String info prompt information <br>* @param Array arrPerson object array <br>*/ <br>function showPersonInfo(info,arrPerson) <br>{ <br>$.each(arrPerson, function(index,callback){ <br>info ="Person id:" arrPerson[index ].id " name:" arrPerson[index].name " sex:" arrPerson[index].sex " age:" arrPerson[index].age "rt"; <br>}); <br>alert(info) ; <br>} <br>//Test data<br>var arrPerson=new Array(); <br>var person=new Object(); <br>person.id=1; <br>person.name= "Handsome guy"; <br>person.sex="male"; <br>person.age=30; <br>arrPerson.push(person); <br>person=new Object(); <br>person.id =2; <br>person.name="Meimeijia"; <br>person.sex="Female"; <br>person.age=28; <br>arrPerson.push(person); <br>person= new Object(); <br>person.id=3; <br>person.name="Meimei B"; <br>person.sex="Female"; <br>person.age=20; <br>arrPerson .push(person); <br>//Test deletion<br>showPersonInfo("Original array: t",arrPerson); <br>arrPerson=remove(arrPerson,"id",1); <br>showPersonInfo("After deletion: t",arrPerson); <br>//Test to get<br>arrPerson=get(arrPerson,"id",3); <br>showPersonInfo("Only get the element with ID 3: rt",arrPerson); <br></script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn