jquery Learning 7 functions to manipulate JavaScript objects and collections _jquery

Source: Internet
Author: User

Delete string trailing null character: $.trim ()

Similar functions are provided in many advanced languages, and the jquery class library provides such functions as well. Specific usage: $.trim (value) deletes the trailing and ending whitespace characters from the passed-in string and returns the result.

To iterate over attributes and collections:

The following methods can be used to manipulate arrays and objects in javascript:

var anarray = [' One ', ' two ', ' three '];

for (var n = 0; n < anarray.length n++) {...}

var anobject = {one:1, two:2, three:3};

For (var p in AnObject) {...}

In jquery, $.each (Container,callback) is provided to iterate over each item of the incoming container, calling the incoming callback function for each item.

This function can iterate over an algebraic group or object in the same format:

var anarray = [' One ', ' two ', ' three '];

$.each (Anarray,function (n,value) {...});

var anobject = {one:1, two:2, three:3};

$.each (Anobject,function (name,value) {...});

To filter an array:

Traversing an array to find elements that match a particular standard is a frequent requirement for processing large amounts of data, and jquery provides the $.grep () function to implement such a function.

$.grep (Array,callback,invert) traverses the array passed in and invokes the callback function for each element. The return value of the callback function determines whether the current element is collected into a new array (the new array is returned as the value of the $.grep () function).

If you want to filter an array, get all values greater than 100:

var bignumber = $.grep (originalarray,function (value) {return value > 100;});

Whether the array contains a specific value or a small value in the array for a particular value:

$.inarray (Value,array) returns the subscript for the first occurrence of the passed value in the array.

var index = $.inarray (2,[1,2,3,4,5]); The result is to return the subscript value of 1 and assign to the index variable.

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.