js怎么遍历数组中的对象的属性?

js怎么遍历数组中的对象的属性?,第1张

具体代码如下所示:

1、<script>  //----------------for用来遍历数组对象;

2、 var i,myArr = [1,2,3]  for (var i = 0i <myArr.lengthi++) {    console.log(i+":"+myArr[i])  }

3、 //---------for-in 用来遍历非数组对象   var man ={hands:2,legs:2,heads:1}  //为所有的对竖扮孙象添加clone方法,即给内置原型(object,Array,function)增加原型属性,该方法很强大,也缺清很危险   if(typeof Object.prototype.clone ==="undefined"){    Object.prototype.clone = function(){}   } ;

4、  //   for(var i in man){    if (man.hasOwnProperty(i)) { //filter,只输出man的私有属性     console.log(i,":",man[i])   }  }  ;

5、//输出结果为print hands:2,legs:2,heads:1   for(var i in man) {//不使用过滤    console.log(i,":",man[i]) 余链 }   ;

6、 //输出结果为://hands : 2 index.html:20   //legs : 2 index.html:20   //heads : 1 index.html:20   //clone : function ;

7、for(var i in man) {    if(Object.prototype.hasOwnProperty.call(man,i)) { //过滤     console.log(i,":",man[i])   }   };

8、 //输出结果为print hands:2,legs:2,heads:1 </script>   。

javaScript遍历对象总结:

1、

2、

concat()连接两个或更启老多的数组,并返回结果。

join()把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔。

pop()删除掘肢并返回数组的最后一个元素

push()向数组的末尾添加一个或更多元素,并返回新的长度。

reverse()颠倒数组中元素的顺序。

shift()删除判旁世并返回数组的第一个元素。

slice()从某个已有的数组返回选定的元素等等。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12514363.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存