添加功能,以JavaScript的Array类减免循环 [英] Adding functions to javascript's Array class breaks for loops

查看:92
本文介绍了添加功能,以JavaScript的Array类减免循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方式来最大/最小值函数添加到JavaScript的Array类,这似乎是一个解决的问题:的 JavaScript的:最小和放大器;最大数组值?。然而,当我试图用,我开始从我的code收到错误信息。事实证明,这种做法不符合循环工作。

 的(我在myarray的){执行console.log(I)} 打印出
1

2

3

最大


有另一种方法,我可以使用?


解决方案

为中... 循环用于通过一个对象的属性循环

。如果你想从你的阵列获取值,你可以这样做:

 为(VAR I = 0; I< myArray.length;我++)
{
    的console.log(myarray的[I])
}

I was looking for a way to add max/min functions to JavaScript's Array class, which seemed to be a solved problem: JavaScript: min & max Array values?. However, when I tried using that, I started getting errors from my code. It turns out that this approach doesn't work with loops.

for(i in myArray) { console.log(i) } 

prints out


1
2
3
max
min

Is there another approach I can use?

解决方案

The for...in loop is used for looping through properties of an object. If you want to get the values from your array, you can do this:

for (var i = 0; i < myArray.length; i++)
{
    console.log(myArray[i])
}

这篇关于添加功能,以JavaScript的Array类减免循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆