javascript 伪数组和数组转换
function ArrayExtend() { this.ArrayExtend = ""; } ArrayExtend.prototype = { jqObjToArray: function (Obj) { var slice = Array.prototype.slice; return slice.call(Obj, 0); }, ArrayToJqObj: function (ary) { var obj = {}, push = Array.prototype.push; push.apply(obj, ary); return obj; } }
var jqObj = { 0: 'tsp', 1: 'sp', 2: '_tsp', length: 3 }, ary = ['tsp', 'sp', '_tsp']; var arry = new ArrayExtend(); console.log(arry.jqObjToArray(jqObj)); console.log(arry.ArrayToJqObj(ary));
运行结果:
js apply 和call介绍:这里借鉴网上找的几篇文章 分享下
http://www.cnitblog.com/yemoo/archive/2007/11/30/37070.aspx
http://www.cnblogs.com/snandy/archive/2011/06/08/2074086.html
http://www.cnblogs.com/sunwangji/archive/2006/08/21/482341.html
这篇对学习过net的或者java的朋友理解起来比前面的3片 或许好点:
http://ll-feng.iteye.com/blog/599108
附加:josn转化数组 利用js for in
var arr = []; var tsp ={ "a": "tsp", "b":"sp", "c": "_sp" } for(var itme in tsp ){ if(tsp[itme]!==undefined){ arr.push(tsp[itme]); } } 3 for(var i=0;i<arr.length;i++){console.log(arr[i])}
求个数:不推荐object上扩展
var arry = [{ t1: 1, t2: 2, t3: 2 }, { t1: 1, t2: 2 }]; Object.prototype.count = function(){ var i = 0; for (var x in this) { this.hasOwnProperty(x) && i++; } return i; };
结果: