码迷,mamicode.com
首页 > 其他好文 > 详细

jq遍历list和object

时间:2017-10-16 22:12:35      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:console   objects   对象   index   lte   array   logs   obj   list   

<script>
    //----------------for用来遍历数组对象--
    var i,myArr = [1,2,3];
    for (var i = 0; i < myArr.length; i++) {
        console.log(i+":"+myArr[i]);
    };
    //---------for-in 用来遍历非数组对象
    var man ={hands:2,legs:2,heads:1};
    //为所有的对象添加clone方法,即给内置原型(object,Array,function)增加原型属性,该方法很强大,也很危险
    if(typeof Object.prototype.clone ==="undefined"){
        Object.prototype.clone = function(){};    
    }
    //
    for(var i in man){
        if (man.hasOwnProperty(i)) { //filter,只输出man的私有属性
            console.log(i,":",man[i]);
        };
    }
    //输出结果为print hands:2,legs:2,heads:1

    for(var i in man) {//不使用过滤
        console.log(i,":",man[i]);
    }    
    //输出结果为
    //hands : 2 index.html:20
    //legs : 2 index.html:20
    //heads : 1 index.html:20
    //clone : function (){} 
    for(var i in man) {
        if(Object.prototype.hasOwnProperty.call(man,i)) { //过滤
            console.log(i,":",man[i]);
        }
    }

   //输出结果为print hands:2,legs:2,heads:1

</script>

另外还有:

$.each(objects, function (i, n) {
    // i表示objects的索引, n表示该索引对应的对象,即objects[i]
});

 

jq遍历list和object

标签:console   objects   对象   index   lte   array   logs   obj   list   

原文地址:http://www.cnblogs.com/miaoying/p/7678423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!