javascript扩展Array类
来源:互联网 发布:linux同步文件夹 编辑:程序博客网 时间:2024/05/20 06:41
1、用于清空数组
Array.prototype.clear = function() { this.length = 0;}
2、判断数据项在数组中的位置
varoldArrayIndexOf = Array.indexOf;//判断是否原始浏览器是否存在indexOf方法Array.prototype.indexOf = function(obj) { if(!oldArrayIndexOf) { for(vari = 0, imax = this.length; i < imax; i++) { if(this[i] === obj) { returni; } } return-1; } else{ returnoldArrayIndexOf(obj); }}
3、判断数据项是否在该数组中
Array.prototype.contain = function(obj) { returnthis.indexOf(obj) !== -1;}
4、把数据项添加到指定的位置
Array.prototype.insertAt = function(index, obj) { if(index < 0) index = 0; if(index > this.length) index = this.length; this.length++; for(vari = this.length - 1; i > index; i--) { this[i] = this[i - 1]; } this[index] = obj;}
5、返回最有一项数据
Array.prototype.last = function() { returnthis[this.length - 1];}
6、移除数组指定索引的值
Array.prototype.removeAt = function(index) { if(index < 0 || index >= this.length) return; varitem = this[index]; for(vari = index, imax = this.length - 2; i < imax; i++) { this[i] = this[i + 1]; } this.length--; returnitem; }
7、移除数据项的数据
Array.prototype.removeAt = function(obj) { varindex = this.indexOf(obj); if(index >= 0) this.removeAt(index);}
8、用于数组的查询
用于查询对象数组中对象的某些值,同时支持对已查询属性进行重命名,若查询的属性不在改数组中,则该属性返回为undefined
Array.prototype.select = function(args) { varnewItems = []; if(typeof(args) === "object"&& arguments.length === 1) {//传入查询的参数为对象时的处理方式 for(vari = 0, imax = this.length; i < imax; i++) { varitem = {}; for(varkey inargs) { if(args[key] !== undefined) { item[key] = this[i][key] === undefined ? "undefined": this[i][key]; } } newItems.push(item); } } elseif(typeof(args) === "string"&& arguments.length === 1) {//传入参数为字符串,且只有一个参数的处理方式 for(vari = 0, imax = this.length; i < imax; i++) { varitem = {}; varkeys = args.split(','); for(vark = 0, kmax = keys.length; k < kmax; k++) { variKey = keys[k].split("as"); if(iKey.length === 1) { item[iKey[0].trim()] = this[i][iKey[0].trim()] === undefined ? "undefined": this[i][iKey[0].trim()]; } else{ item[iKey[1].trim()] = this[i][iKey[0].trim()] === undefined ? "undefined": this[i][iKey[0].trim()]; } } newItems.push(item); } } else{//传入的参数是多个字符串的处理方式 for(vari = 0, imax = this.length; i < imax; i++) { varitem = {}; for(varj = 0, jmax = arguments.length; j < jmax; j++) { if(arguments[j] !== undefined) { variKey = arguments[j].split("as"); if(iKey.length === 1) { item[iKey[0].trim()] = this[i][iKey[0].trim()] === undefined ? "undefined": this[i][iKey[0].trim()]; } else{ item[iKey[1].trim()] = this[i][iKey[0].trim()] === undefined ? "undefined": this[i][iKey[0].trim()]; } } } newItems.push(item); } } returnnewItems;}
假设数据对象数组为:var obj = [{ name: "张三", second.age: "24", sex: "男" }
, { name: "张二", age: "21", sex: "女" }
, { name: "张一", age: "23", sex: "男" }
, { name: "张四", age: "25", sex: "女" }
, { name: "张五", age: "22", sex: "男"}];
例一:obj.select(“name,second.age as age,sex”);
例二:obj.select(“name”,”second.age as age”,”sex”);
例三:查询属性对象var arg={name:”",second.age:”",sex:”"};
obj.select(arg);
- javascript扩展Array类
- javascript中扩展Array类的原型
- javascript给String类和Array扩展常用方法
- JavaScript Array 可以扩展的几个方法
- Javascript数组Array的some扩展函数
- Javascript数组Array的forEach扩展方法
- Dojo 扩展 javascript 核心库 - 数组方法(Array)
- 收集的几个javascript的date和array扩展
- JavaScript内置对象Array的扩展(去重,排序,合并)
- ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展
- ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展(实例)
- Javascript本地类型--array类
- JavaScript数组(Array 类)详解
- JavaScript——Array类
- 扩展javascript扩展(类,对象,原型)
- [Javascript Array] Array
- JavaScript-Array & Array like
- JavaScript Array
- oracle wmsys.wm_concat(column)函数的使用
- 三种加密算法和两种密钥交换机制讲解
- 在JBoss AS7中配置PostgreSQL数据源
- 随笔:手机尾号问题
- eclipse-jee-galileo-SR2 如何配置eclipse.ini
- javascript扩展Array类
- 全是老古董:俄罗斯程序员收藏的8080处理器
- .net实现生成及打印条形码功能
- 从小米盒子看高清播放器市场
- 基于visual c++之windows核心编程代码分析 实现开机自启动
- Swing:绘制渐变的饼图
- 2012年互联网创业者生存与发展报告
- c++实现哈夫曼编码完整代码
- Linux(CentOS 5.5) Redis 安装及RedisPHP拓展安装应用