Use jQuery to operate on the object array implementation code

Source: Internet
Author: User

JQuery provides the following tools for array element operations:
(1) Examples of arrays and objects: jQuery. each (obj, callback)
The general example method can be used for example object and array. The callback function has two parameters: the first is the object's member or array index, and the second is the corresponding variable or content. If you want to exit the each loop, the callback function returns false. Other return values are ignored.
(2) filter array elements: jQuery. grep (array, callback, [invert])
Filter Array elements using the filter function. This function must pass 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 delete the element.
(3) Search for array elements: jQuery. inArray (value, array)
Determine the position of the first parameter in the array (-1 is returned if no value is found ).
(4) Delete duplicate elements: jQuery. unique (array)
Delete repeated elements in the array.
The following instances filter through the object array to obtain or delete the array elements with the specified attribute as the specified value.
Copy codeThe Code is as follows:
<Script src = "js/jquery. js"> </script>
<Script>
/**
* Delete an object whose attribute is objPropery and whose value is objValue from the object array.
* @ Param Array arrPerson Array object
* @ Param String the attributes of the objPropery object
* @ Param String the value of the objPropery object
* @ Return Array: filtered Array
*/
Function remove (arrPerson, objPropery, objValue)
{
Return $. grep (arrPerson, function (cur, I ){
Return cur [objPropery]! = ObjValue;
});
}
/**
* Get the object whose attribute is objPropery and whose value is objValue from the object Array
* @ Param Array arrPerson Array object
* @ Param String the attributes of the objPropery object
* @ Param String the value of the objPropery object
* @ Return Array the filtered Array
*/
Function get (arrPerson, objPropery, objValue)
{
Return $. grep (arrPerson, function (cur, I ){
Return cur [objPropery] = objValue;
});
}
/**
* Display object array information
* @ Param String info prompt information
* @ Param Array arrPerson object Array
*/
Function showPersonInfo (info, arrPerson)
{
$. Each (arrPerson, function (index, callback ){
Info + = "Person id:" + arrPerson [index]. id + "name:" + arrPerson [index]. name + "sex:" + arrPerson [index]. sex + "age:" + arrPerson [index]. age + "\ r \ t ";
});
Alert (info );
}
// Test Data
Var arrPerson = new Array ();
Var person = new Object ();
Person. id = 1;
Person. name = "handsome guy ";
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 = "Mei Yi ";
Person. sex = "female ";
Person. age = 20;
ArrPerson. push (person );
// Test Deletion
ShowPersonInfo ("original array: \ r \ t", arrPerson );
ArrPerson = remove (arrPerson, "id", 1 );
ShowPersonInfo ("after deletion: \ r \ t", arrPerson );
// Obtain the test result
ArrPerson = get (arrPerson, "id", 3 );
ShowPersonInfo ("Get only elements with ID 3: \ r \ t", arrPerson );
</Script>

Related Article

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.