当前位置: 首页 > 图文教程 > 网络编程 > Javascript > jQuery学习7 操作JavaScript对象和集合的函数

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 jQuery学习7 操作JavaScript对象和集合的函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-02-27   浏览: 397 ::
收藏到网摘: n/a

jQuery学习7:操作JavaScript对象和集合的函数

删除字符串首尾空字符:$.trim()

像很多高级语言都提供了类似的函数,jQuery类库也提供了这样的函数。具体用法:$.trim(value)从已传入的字符串里删除首尾空白字符并返回结果。

对属性和集合进行迭代:

在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){...}

在jQuery中提供$.each(container,callback) 对传入的容器的每一项进行迭代,为每一项调用传入的回调函数。

这个函数可以用相同的格式来迭代数组或对象:

var anArray = ['one','two','three'];

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

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

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

对数组进行筛选:

遍历数组以便查找匹配特定标准的元素,是处理大量数据的应用的频繁需求,jQuery提供了$.grep()函数实现此类功能。

$.grep(array,callback,invert) 遍历已传入的数组,为各元素分别调用回调函数。回调函数的返回值决定是否把当前元素收集到新数组(新数组作为$.grep()函数的值而被返回)。

如果想要筛选一个数组,获取所有大于100的值:

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

数组中是否包含特定值或是特定值在数组中的小标值:

$.inArray(value,array) 返回已传入的值在数组里第一次出现时的下标。

var index = $.inArray(2,[1,2,3,4,5]); 结果是返回下标值1并指派到index变量。