是否可以像下面传统的 for 循环一样创建一个嵌套的 for...of 循环?
Is it possible to create a nested for...of loop in the same way as the traditional for loop below?
我正在尝试使用 for...of 循环复制此选择排序 for 循环。我已经努力过渡了一段时间,但是,我尝试过的任何方法都没有奏效。这可能吗?
数组集:
var fruits = ["Apple", "Banana", "Carrot", "Guava", "Orange"];
传统for循环
for (var i = 0; i < fruits; i++) {
let outerItem = fruits[i];
for (var j = i + 1; j < fruits.length; j++) {
let innerItem = fruits[j];
if (innerItem == outerItem) {
innerItem.length > outerItem.length ? fruits.splice(fruits.indexOf(outerItem), 1) :
fruits.splice(fruits.indexOf(innerItem), 1);
}
}
}
for...of 循环
for(let outerItem of fruits)
{
for(let innerItem of fruits)
{
}
}
要快速复制数组,请使用切片:const sliced = fruits.slice();
下面显示了 Traditional for loop
和 for...of loop
在给定数组 var fruits = ["Apple", "Banana", "Carrot", "Guava", "Orange"];
上的解决方案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const writeln = (text = '') => document.writeln(text + '<br/>');
</script>
<script>
var fruits = ["Apple", "Banana", "Carrot", "Guava", "Orange"];
writeln(fruits);
writeln();
writeln('Slice');
const sliced = fruits.slice();
writeln(sliced);
writeln();
writeln('Traditional for loop');
const copy = [];
for (var i = 0; i < fruits.length; i++) {
copy.push(fruits[i]);
}
writeln(copy);
writeln();
writeln('for...of loop');
const copy1 = [];
for (const value of fruits) {
copy1.push(value);
}
writeln(copy1);
</script>
</body>
</html>
我正在尝试使用 for...of 循环复制此选择排序 for 循环。我已经努力过渡了一段时间,但是,我尝试过的任何方法都没有奏效。这可能吗?
数组集:
var fruits = ["Apple", "Banana", "Carrot", "Guava", "Orange"];
传统for循环
for (var i = 0; i < fruits; i++) {
let outerItem = fruits[i];
for (var j = i + 1; j < fruits.length; j++) {
let innerItem = fruits[j];
if (innerItem == outerItem) {
innerItem.length > outerItem.length ? fruits.splice(fruits.indexOf(outerItem), 1) :
fruits.splice(fruits.indexOf(innerItem), 1);
}
}
}
for...of 循环
for(let outerItem of fruits)
{
for(let innerItem of fruits)
{
}
}
要快速复制数组,请使用切片:const sliced = fruits.slice();
下面显示了 Traditional for loop
和 for...of loop
在给定数组 var fruits = ["Apple", "Banana", "Carrot", "Guava", "Orange"];
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const writeln = (text = '') => document.writeln(text + '<br/>');
</script>
<script>
var fruits = ["Apple", "Banana", "Carrot", "Guava", "Orange"];
writeln(fruits);
writeln();
writeln('Slice');
const sliced = fruits.slice();
writeln(sliced);
writeln();
writeln('Traditional for loop');
const copy = [];
for (var i = 0; i < fruits.length; i++) {
copy.push(fruits[i]);
}
writeln(copy);
writeln();
writeln('for...of loop');
const copy1 = [];
for (const value of fruits) {
copy1.push(value);
}
writeln(copy1);
</script>
</body>
</html>