在写这篇文章之前,xxx已经写过了几篇关于改数组查询主题的文章,想要了解的朋友可以去翻一下之前的文章
1、于用清空数组
Array.prototype.clear = function() {
this.length = 0;
}
2、判断数据项在数组中的位置
var oldArrayIndexOf = Array.indexOf;//判断是不是原始浏览器是不是存在indexOf法方
Array.prototype.indexOf = function(obj) {
if (!oldArrayIndexOf) {
for (var i = 0, imax = this.length; i < imax; i++) {
if (this[i] === obj) {
return i;
}
}
return -1;
} else {
return oldArrayIndexOf(obj);
}
}
3、判断数据项是不是在该数组中
Array.prototype.contain = function(obj) {
return this.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 (var i = this.length - 1; i > index; i--) {
this[i] = this[i - 1];
}
this[index] = obj;
}
5、回返最有一项数据
Array.prototype.last = function() {
return this[this.length - 1];
}
6、移除数组指定索引的值
Array.prototype.removeAt = function(index) {
if (index < 0 || index >= this.length) return;
var item = this[index];
for (var i = index, imax = this.length - 2; i < imax; i++) {
this[i] = this[i + 1];
}
this.length--;
return item;
}
7、移除数据项的数据
Array.prototype.removeAt = function(obj) {
var index = this.indexOf(obj);
if (index >= 0)
this.removeAt(index);
}
8、于用数组的查询
于用查询象对数组中象对的某些值,同时支撑对已查询属性行进重命名,若查询的属性不在改数组中,则该属性回返为undefined
Array.prototype.select = function(args) {
var newItems = [];
if (typeof (args) === "object" && arguments.length === 1) {//传入查询的参数为象对时的处理方式
for (var i = 0, imax = this.length; i < imax; i++) {
var item = {};
for (var key in args) {
if (args[key] !== undefined) {
item[key] = this[i][key] === undefined ? "undefined" : this[i][key];
}
}
newItems.push(item);
}
} else if (typeof (args) === "string" && arguments.length === 1) {//传入参数为字符串,且只有一个参数的处理方式
for (var i = 0, imax = this.length; i < imax; i++) {
var item = {};
var keys = args.split(',');
for (var k = 0, kmax = keys.length; k < kmax; k++) {
var iKey = 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 (var i = 0, imax = this.length; i < imax; i++) {
var item = {};
for (var j = 0, jmax = arguments.length; j < jmax; j++) {
if (arguments[j] !== undefined) {
var iKey = 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);
}
}
return newItems;
}
假设数据象对数组为: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);
文章结束给大家分享下程序员的一些笑话语录: 那是习惯决定的,一直保持一个习惯是不好的!IE6的用户不习惯多标签,但是最终肯定还是得转到多标签的浏览器。历史(软件UI)的进步(改善)不是以个人意志(习惯)为转移的!