我如何通过一个序列中的所有输入,使用JavaScript?
当前回答
Lambda Syntax 通常不适用于 Internet Explorer 10 或更低版本。
我通常使用
[].forEach.call(arrayName,function(value,index){
console.log("value of the looped element" + value);
console.log("index of the looped element" + index);
});
如果您是一个 jQuery 粉丝,并且已经有一个 jQuery 文件运行,您应该逆转指数和值参数的位置。
$("#ul>li").each(function(**index, value**){
console.log("value of the looped element" + value);
console.log("index of the looped element" + index);
});
其他回答
我知道这是一个古老的帖子,而且已经有很多很好的答案。 为了更完整的我想象我会扔在另一个使用AngularJS. 当然,这只适用于如果你使用Angular,显然,然而,我会喜欢把它无论如何。
第一個論點是對象(命令)以 iterate 上,第二個論點是 iterator 函數,而選擇的第三個論點是對象背景(基本上提到在輪內的「這」)。
var temp = [1, 2, 3];
angular.forEach(temp, function(item) {
//item will be each element in the array
//do something
});
var temp = [1, 2, 3];
var temp2 = [];
angular.forEach(temp, function(item) {
this.push(item); //"this" refers to the array passed into the optional third parameter so, in this case, temp2.
}, temp2);
angular.forEach(temp, function(item) {
temp2.push(item);
});
福利
易于阅读 易于写作 如果可用, angular.forEach 将使用 ES5 forEach loop. 现在,我会得到效率,因为 forEach loop 比 loops 更慢。
angular.forEach(obj1.results, function(result1) {
angular.forEach(obj2.results, function(result2) {
if (result1.Value === result2.Value) {
//do something
}
});
});
//exact same with a for loop
for (var i = 0; i < obj1.results.length; i++) {
for (var j = 0; j < obj2.results.length; j++) {
if (obj1.results[i].Value === obj2.results[j].Value) {
//do something
}
}
}
我會說,為 / 是走的路:
const arr = ['a', 'b', 'c']; for (const v of arr) { console.log(v); // 打印“a”,“b”,“c” }
例如,如果您设置 arr.foo = 'test', for (var v in arr) 将通过 'foo' 密钥旋转. 不同于 forEach(), for/of 不会错过'洞' 在 序列. const arr = ['a', 'c'] 是有效的 JavaScript, 只有 2 个元素是'洞'. 序列功能相当于 ['a', undefined, 'c'].
您可以在此博客中阅读更多关于 for/of vs forEach( )的文章。
关于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]
你可以这样打电话给每个人:
forEach 将在您提供的序列上进行 iterate 并为每个 iteration 将有保持该 iteration 的值的元素. 如果您需要索引,您可以通过 i 作为 forEach 的呼叫回复函数中的第二个参数获得当前指数。
Foreach 基本上是一种高顺序函数,它作为其参数需要另一个函数。
let theArray= [1,3,2];
theArray.forEach((element) => {
// Use the element of the array
console.log(element)
}
出口:
1
3
2
你也可以在这样的序列上进行 iterate:
for (let i=0; i<theArray.length; i++) {
console.log(i); // i will have the value of each index
}