你如何获得循环计数器/索引在JavaScript中的语法使用for ...? [英] How do you get the loop counter/index using a for…in syntax in JavaScript?

查看:163
本文介绍了你如何获得循环计数器/索引在JavaScript中的语法使用for ...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不要使用for ... in迭代数组,使用它遍历对象的属性
也就是说,这个问题仍然适用于...循环。






我明白在JavaScript语法中的基本...看起来像这样:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

但是如何获得循环计数器/索引?我知道我可以这样做:

$ p $ var $ i $ $ $ $ $ $ $
alert(i)
i ++
}

,旧:

  var i 
(i = 0; 1< myArray.length; i ++){
var obj = myArray [i]
alert(i)
}



<但我宁愿使用更简单的for-in循环。我认为他们看起来更好,更有意义。



但是有一个更简单或更优雅的方式吗?

在Python中很简单:
$ b $ pre $ 对于枚举中的obj(myArray):
print i


解决方案

for ... in 迭代属性名称,而不是值,这样做以未指定的顺序(即使在ES6之后)。你不应该用它来遍历数组。对于他们来说,ES5的 forEach 方法可以将值和索引传递给你给它的函数:

  var myArray = [123,15,187,32]; 

myArray.forEach(function(value,i){
console.log('%d:%s',i,value);
});

//输出:
// 0:123
// 1:15
// 2:187
// 3:32

或ES6的 Array.prototype.entries

pre $ for(const [i,value] of myArray.entries()){
console.log(' d:%s',i,value);



$ b

对于一般的iterables(你可以使用 for ... 循环而不是 for ... in ),但是没有任何内置的内容:

  function * enumerate(iterable){
let i = 0;

(可迭代的const x){
yield [i,x];
i ++; ($,



(enumerate(myArray)的const [i,obj]){
console.log(i,obj);

演示 for ... in - 枚举属性 - 你需要一个额外的计数器。 Object.keys(obj).forEach 可以工作,但只包含自己的属性; for ... in 包含原型链上任何位置的枚举属性。


Caution. Don't use for…in to iterate over an array, use it to iterate over the properties of an object.
That said, this question still applies to for…of loops.


I understand that the basic for…in syntax in JavaScript looks like this:

for (var obj in myArray) {
    // ...
}

But how do I get the loop counter/index? I know I could probably do something like:

var i = 0
for (var obj in myArray) {
    alert(i)
    i++
}

Or even the good, old:

var i
for (i = 0; 1 < myArray.length; i++) {
    var obj = myArray[i]
    alert(i)
}

But I would rather use the simpler for-in loop. I think they look better and make more sense.

But is there a simpler or more elegant way?

In Python it's easy:

for i, obj in enumerate(myArray):
    print i

解决方案

for…in iterates over property names, not values, and does so in an unspecified order (yes, even after ES6). You shouldn’t use it to iterate over arrays. For them, there’s ES5’s forEach method that passes both the value and the index to the function you give it:

var myArray = [123, 15, 187, 32];

myArray.forEach(function (value, i) {
    console.log('%d: %s', i, value);
});

// Outputs:
// 0: 123
// 1: 15
// 2: 187
// 3: 32

Or ES6’s Array.prototype.entries, which now has support across current browser versions:

for (const [i, value] of myArray.entries()) {
    console.log('%d: %s', i, value);
}

For iterables in general (where you would use a for…of loop rather than a for…in), there’s nothing built-in, however:

function* enumerate(iterable) {
    let i = 0;

    for (const x of iterable) {
        yield [i, x];
        i++;
    }
}

for (const [i, obj] of enumerate(myArray)) {
    console.log(i, obj);
}

demo

If you actually did mean for…in – enumerating properties – you would need an additional counter. Object.keys(obj).forEach could work, but it only includes own properties; for…in includes enumerable properties anywhere on the prototype chain.

这篇关于你如何获得循环计数器/索引在JavaScript中的语法使用for ...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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