我如何通过一个序列中的所有输入,使用JavaScript?


当前回答

可能為(i = 0; i < array.length; i++) loop 不是最好的選擇. 為什麼? 如果你有這個:

var array = new Array();
array[1] = "Hello";
array[7] = "World";
array[11] = "!";

方法将从序列(0)到序列(2). 首先,这将是你甚至没有的参考变量,第二,你不会有序列中的变量,第三,这将使代码泡沫。

for(var i in array){
    var el = array[i];
    //If you want 'i' to be INT just put parseInt(i)
    //Do something with el
}

如果你想要它成为一个功能,你可以这样做:

function foreach(array, call){
    for(var i in array){
        call(array[i]);
    }
}

如果你想打破,更有逻辑:

function foreach(array, call){
    for(var i in array){
        if(call(array[i]) == false){
            break;
        }
    }
}

例子:

foreach(array, function(el){
    if(el != "!"){
        console.log(el);
    } else {
        console.log(el+"!!");
    }
});

它回来了:

//Hello
//World
//!!!

其他回答

我也想添加这个作为一个逆转旋转的组成和上面的答案,谁也喜欢这个合成。

var foo = [object,object,object];
for (var i = foo.length, item; item = foo[--i];) {
    console.log(item);
}

福利:

这一点的好处:你已经在第一个类似的参考,不需要以后与另一个线声明。

麻烦:

这将打破,每当参考是虚假 - 虚假(不定义,等)。它可以作为一个优点,但是,它会使它稍微更难阅读。

如果你想使用 forEach(),它会看起来像 -

theArray.forEach ( element => {
    console.log(element);
});

如果你想使用(),它会看起来像 -

for(let idx = 0; idx < theArray.length; idx++){
    let element = theArray[idx];
    console.log(element);
}

如果您想保持代码的功能,请使用地图:

theArray.map(instance => do_something);

在这种情况下,您将为未来的操作创建一个新的序列,并将错过任何不需要的副作用。

ECMAScript 5(JavaScript版本)与Arrays一起工作:

forEach - 通过序列中的每个项目,并与每个项目所需的一切。

['C', 'D', 'E'].forEach(function(element, index) {
  console.log(element + " is #" + (index+1) + " in the musical scale");
});

// Output
// C is the #1 in musical scale
// D is the #2 in musical scale
// E is the #3 in musical scale

在这种情况下,更有兴趣使用一些内置功能在序列上运行。

地图 - 它创建一个新的序列,结果是呼叫回复功能. 这个方法是很好的使用,当你需要格式化的元素的序列。

// Let's upper case the items in the array
['bob', 'joe', 'jen'].map(function(elem) {
  return elem.toUpperCase();
});

// Output: ['BOB', 'JOE', 'JEN']

减少 - 正如名称所说,它将序列减少到一个单一值,通过在当前元素中传输的特定函数和以前的执行结果。

[1,2,3,4].reduce(function(previous, current) {
  return previous + current;
});
// Output: 10
// 1st iteration: previous=1, current=2 => result=3
// 2nd iteration: previous=3, current=3 => result=6
// 3rd iteration: previous=6, current=4 => result=10

每次 - 返回真实或虚假,如果序列中的所有元素在呼叫回复函数中通过测试。

// Check if everybody has 18 years old of more.
var ages = [30, 43, 18, 5];
ages.every(function(elem) {
  return elem >= 18;
});

// Output: false

过滤器 - 非常相似于每一个,除了过滤器返回一个序列的元素返回真实的给定的功能。

// Finding the even numbers
[1,2,3,4,5,6].filter(function(elem){
  return (elem % 2 == 0)
});

// Output: [2,4,6]

根据新更新 ECMAScript 6 (ES6) 和 ECMAScript 2015 的功能,您可以使用以下选项:

对于洛普

for(var i = 0; i < 5; i++){
  console.log(i);
}

// Output: 0,1,2,3,4

上一篇:在洛普斯

let obj = {"a":1, "b":2}

for(let k in obj){
  console.log(k)
}

// Output: a,b

Array.forEach( )

let array = [1,2,3,4]

array.forEach((x) => {
  console.log(x);
})

// Output: 1,2,3,4

為...LOPS

let array = [1,2,3,4]

for(let x of array){
  console.log(x);
}

// Output: 1,2,3,4

當LOPS

let x = 0

while(x < 5){
  console.log(x)
  x++
}

// Output: 1,2,3,4

此分類上一篇: while loops

let x = 0

do{
  console.log(x)
  x++
}while(x < 5)

// Output: 1,2,3,4