javascript操作对象和集合
//6.3.2 遍历属性和集合 对象与数组
var arr = [ "one", "two", "three", "four"];
$.each(arr, function(i,value){
console.log(this,i,value);
});
var obj = { one:1, two:2, three:3, four:4};
$.each(obj, function(key, val) {
console.log(obj[key],key,val);
});
//javascript操作对象和集合
//删除传入字符串开头和结尾处的空白字符,并返回修改后的结果
//6.3.1 修剪字符串
//$.trim(value);
console.log( $.trim(" hello, how are you? "));
$.trim(" hello, how are you? "); // "hello, how are you?"
console.log( $.trim( 1111 ));
/** 6.3.3 筛选数组 $.grep(array,callback,invert)
* invert的参数被省略或者为false,回调函数返回true将导致数据被收集,invert的参数被省略或者为true,回调函数返回false将导致数据被收集
*/
//example
var bigNumbers = $.grep(originalArray,function(value){
return value > 100; //帅选数组大于100的值
});
var badZips = $.grep(originalArray,function(value){
return value.match(/^d{5}(-d{4})?$/) != null; //帅选数组大于100的值
},true);
//6.3.4 转换数组 $.map(array,callback)
var oneBased = $.map([0,1,2,3,4],function(value){ return value+1;});
console.log(oneBased)
var strings = ['1','2','3','4','S','6'];
var values = $.map(strings,function(value){
var result = new Number(value);
console.log(result);
return isNaN(result) ? null : result;
});
console.log(values);
var charactters = $.map(['that','this','other thing'],function(value){
return value.split('');
});
console.log(charactters);
//6.3.5 发现javascript数组的更多乐趣
//$.inArray(value,array)返回传入的值第一次出现的小下标
var index = $.inArray(2,[1,2,3,4,5]);
console.log(index);
// $.makeArray(object) 将传入的类似数组对象转换为JavaScript 返回值JavaScript数组
//$.unique(array) array(数组)需要检查的DOM的元素数组,返回值:返由在传入的数组里不重复的元素组成DOM元素数组
//$.merge(array1,array2) 返回值:合并后的第一个数组
未完待续