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


当前回答

根据新更新 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

其他回答

某些 C 风格的语言使用 foreach 通过列表旋转。 在 JavaScript 中,这与 for..in 旋转结构进行:

var index,
    value;
for (index in obj) {
    value = obj[index];
}

要避免通过对象的原型继承的阅读值,只需检查是否属于对象的财产:

for (i in obj) {
    if (obj.hasOwnProperty(i)) {
        //do stuff
    }
}

此外,ECMAScript 5 已添加一个 forEach 方法到 Array.prototype 可以用来列出一个序列,使用一个字符串(多字符串在文件中,所以你仍然可以使用它在更老的浏览器):

arr.forEach(function (val, index, theArray) {
    //do stuff
});

重要的是要注意的是,当通话返回虚假时,Array.prototype.forEach 不会破裂。 jQuery 和 Underscore.js 提供各自的变量,以提供可以短循环的漏洞。

注意: 这个答案是毫无希望的日期。 为了更现代化的方法,请参见在序列上可用的方法。

forEach 地图过滤器 ZIP 减少每一个


在JavaScript中引用一个序列的标准方式是Vanilla for-loop:

var length = arr.length,
    element = null;
for (var i = 0; i < length; i++) {
  element = arr[i];
  // Do something with element
}

但是,请注意,这种方法只有在你有一个密集的序列,每个指数都被一个元素占据。 如果序列是分散的,那么你可以运行到性能问题与这个方法,因为你会 iterate上很多指标,实际上不存在在序列。 在这种情况下,一个为.. in-loop 可能是一个更好的想法。

在 ECMAScript 5 将有一个 forEach 方法在序列的原型,但它不支持在遗产浏览器. 因此,能够连续使用它,你必须有一个环境支持它(例如,Node.js 服务器侧 JavaScript),或使用一个“Polyfill”。 该功能的 Polyfill 是,然而,三角形,因为它使代码更容易阅读,它是一个好 polyfill。

Mozilla 文档

/* 获取所有表格 */ document.querySelectorAll(“表格” ).forEach(表格 => { /* 每个表格,添加我们提交的事件 */ form.addEventListener(“提交”,事件 => { event.preventDefault(); // Return false /* 显示它 */ alert(event.target.action); console.log(event.target); }; }; ); <form action="form1.php" > <input type="submit" value="Submit" /> </form

关于ECMAScript6:

列表 = [0, 1, 2, 3] for (let obj of list) { console.log(obj) }

在哪里避免与内相关的异常,使它像任何其他语言的旋转一样工作,并让它在旋转中相反地连接到函数内。

手臂({})可以被忽略,如果只有一个命令(例如,在上面的例子)。

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]