相关疑难解决方法(0)

用于索引/键的...的TypeScript?

如此处所述, TypeScript引入了一个foreach循环:

var someArray = [9, 2, 5];
for (var item of someArray) {
    console.log(item); // 9,2,5
}
Run Code Online (Sandbox Code Playgroud)

但是没有任何索引/密钥吗?我希望有类似的东西:

for (var item, key of someArray) { ... }
Run Code Online (Sandbox Code Playgroud)

foreach for-loop typescript

118
推荐指数
5
解决办法
21万
查看次数

在 ES6 javascript 中检测 FOR OF 循环中的最后一次迭代

有多种方法可以找出 a forandfor...in循环的最后一次迭代。但是我如何找到循环中的最后一次迭代for...of。我在文档中找不到。

for (item of array) {
    if (detect_last_iteration_here) {
        do_not_do_something
    }
}
Run Code Online (Sandbox Code Playgroud)

javascript iterator loops for-loop ecmascript-6

20
推荐指数
4
解决办法
2万
查看次数

如何在for循环中访问当前的i数?

给定for循环,赋值变量的值(i对于此示例)array[i]等于如果它是循环的法线则相等.如何i访问当前所在的数组的索引.

我想要的是

let array = ["one", "two", "three"];

for (let i of array) {
  console.log(i);// normally logs cycle one : "one", cycle two : "two", cycle three : "three".
  console.log(/*what equals the current index*/);// what I want to log cycle one : 1, cycle two : 2, cycle three : 3. 
}
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-6

16
推荐指数
3
解决办法
1124
查看次数

我应该如何以索引顺序迭代稀疏数组?

我有一个稀疏数组,其内容不保证以索引顺序插入,但需要按索引顺序迭代.要遍历稀疏数组,我了解您需要使用for..in语句.

但是,根据这篇文章:

无法保证for ... in将以任何特定顺序返回索引

但是像这样的stackoverflow问题表明虽然对象属性订单不能保证,但是数组顺序是:

在JavaScript中不保证对象中的属性顺序,您需要使用数组.

我在Chrome,Firefox和IE的最新版本中对此进行了测试.

<ol id="items"></ol>
Run Code Online (Sandbox Code Playgroud)
var list = [];

function addItem(index) {
    list[index] = { idx : index };
}

var insertOrder = [ 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15 ];

for ( var i = 0; i < 15; i++ ) {
    addItem(insertOrder[i]);
}

for(var item in list) {
    $("#items").append("<li>" + list[item].idx + "</li>");
}
Run Code Online (Sandbox Code Playgroud)

所有人似乎都遵守索引顺序,所以我可以相信这总是如此吗?否则,我如何以索引顺序最好地获取它们?

javascript arrays sparse-array

7
推荐指数
1
解决办法
1793
查看次数

React Native for循环获取索引

这可能是一个超级简单的问题,但我有以下问题:

let groups = [{}, {}, {}];

for(let g of groups) {
    console.log(g);
}
Run Code Online (Sandbox Code Playgroud)

如何获得该组的索引号?最好不进行计数。

javascript ecmascript-6

3
推荐指数
1
解决办法
1万
查看次数