Implementing code _jquery using jquery to manipulate object arrays

Source: Internet
Author: User
jquery provides the following tools primarily for array element operations:
(1) Examples of arrays and objects: Jquery.each (obj,callback)
A universal example method, which can be used for example objects and arrays. The callback function has two parameters: the first is the index of the member or array of the object, and the second is the corresponding variable or content. If you need to exit each loop so that the callback function returns false, the other return values are ignored.
(2) Filtering of array elements: Jquery.grep (Array,callback,[invert])
Filter the array elements using the filter function. This function passes at least two parameters: the array to be filtered and the filter function. The filter function must return true to preserve the element or false to delete the element.
(3) Search for array elements: Jquery.inarray (Value,array)
Determines the position of the first parameter in the array (returns 1 if not found).
(4) Delete duplicate element: Jquery.unique (Array)
Deletes a repeating element in an array.
The following example implements the object array filter to get or delete the array element with the specified property as the specified value.
Copy Code code as follows:

<script src= "Js/jquery.js" ></script>
<script>
/**
* Removes the property from the object array as Objpropery, and the value is the object of the ObjValue element
* @param array Arrperson Object
* @param String Objpropery The properties of the object
* @param String Objpropery The value of the object
* @return Array filtered after
*/
function Remove (arrperson,objpropery,objvalue)
{
Return $.grep (Arrperson, function (cur,i) {
return cur[objpropery]!=objvalue;
});
}
/**
* Gets an object from an array of objects that is objpropery and whose value is objvalue element
* @param array Arrperson Object
* @param String Objpropery The properties of the object
* @param String Objpropery The value of the object
* The array filtered by @return array
*/
function Get (Arrperson,objpropery,objvalue)
{
Return $.grep (Arrperson, function (cur,i) {
return cur[objpropery]==objvalue;
});
}
/**
* Display Object array information
* @param String Info message
* @param array Arrperson An array of objects
*/
function Showpersoninfo (Info,arrperson)
{
$.each (Arrperson, function (index,callback) {
info+= "Person ID:" + arrperson[index].id + "Name:" + arrperson[index].name+ "Sex:" + arrperson[index].sex+ "Age:" + arrper Son[index].age+ "\r\t";
});
alert (info);
}
Test data
var arrperson=new Array ();
var person=new Object ();
person.id=1;
Person.name= "Handsome";
person.sex= "Male";
person.age=30;
Arrperson.push (person);
Person=new Object ();
person.id=2;
Person.name= "Meimei Jia";
person.sex= "female";
person.age=28;
Arrperson.push (person);
Person=new Object ();
person.id=3;
Person.name= "Meimei B";
person.sex= "female";
person.age=20;
Arrperson.push (person);
Test Delete
Showpersoninfo ("Original array: \r\t", Arrperson);
Arrperson=remove (Arrperson, "id", 1);
Showpersoninfo ("After deletion: \r\t", Arrperson);
Test get
Arrperson=get (Arrperson, "id", 3);
Showpersoninfo ("Get only the element with ID 3: \r\t", Arrperson);
</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.