使用箭头函数循环遍历值数组 [英] Loop through array of values with Arrow Function

查看:1687
本文介绍了使用箭头函数循环遍历值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有:

var someValues = [1, 'abc', 3, 'sss'];

如何使用箭头函数循环遍历每个值并对每个值执行操作?

How can I use an arrow function to loop through each and perform an operation on each value?

推荐答案

简而言之:

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

如果你关心索引,那么可以传递第二个参数来接收当前元素的索引: / p>

If you care about index, then second parameter can be passed to receive the index of current element:

someValues.forEach((element, index) => {
    console.log(`Current index: ${index}`);
    console.log(element);
});




请参阅此处以了解有关ES6阵列的更多信息: https://developer.mozilla.org/en-US/docs/Web / JavaScript / Reference / Global_Objects / Array

这篇关于使用箭头函数循环遍历值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆