百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>JS教程> forEach()、Array.map()和Array.filter()如何用?(代码示例)
分享文章到:

forEach()、Array.map()和Array.filter()如何用?(代码示例)

发布时间:09/01 来源:未知 浏览: 关键词:
本篇文章给大家介绍一下forEach()、Array.map()和Array.filter()的用途。有必然的参照 价值,有需要的伴侣可以参照 一下,但愿对大家有所帮忙。

Array.forEach()

forEach()办法为每个数组元素调取一次函数(回调函数)。【相关课程引荐:JavaScript视频教程】

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>JavaScript Array.forEach()</title>
<body>
<h2>JavaScript Array.forEach()</h2>
<p>为每个数组元素调取一次函数。</p>
<p id="demo"></p>
<script>
    var txt = "";
    var numbers = [45, 4, 9, 16, 25];
    numbers.forEach(myFunction);
    document.getElementById("demo").innerHTML = txt;

    function myFunction(value, index, array) {
        txt = txt + value + "<br>";
    }
</script>
</body>
</html>

请留意,该函数有3个参数:

 ● 元素值

 ● 元素索引

 ● 数组本身

上面的示例仅使用value参数。该示例可以重写为:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>JavaScript Array.forEach()</title>
<body>
<h2>JavaScript Array.forEach()</h2>
<p>为每个数组元素调取一次函数。</p>
<p id="demo"></p>
<script>
    var txt = "";
    var numbers = [45, 4, 9, 16, 25];
    numbers.forEach(myFunction);
    document.getElementById("demo").innerHTML = txt;

    function myFunction(value) {
        txt = txt + value + "<br>";
    }
</script>
</body>
</html>

Array.map()

map()办法通过对每个数组元素施行函数来创立新数组。map()办法是不施行没有值的数组元素的函数。map()办法不会更换原始数组。

此示例将每个数组值乘以2:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>JavaScript Array.map()</title>
<body>
<h2>JavaScript Array.map()</h2>
<p>通过对每个数组元素施行函数来创立新数组。</p>
<p id="demo"></p>
<script>
    var numbers1 = [45, 4, 9, 16, 25];
    var numbers2 = numbers1.map(myFunction);
    document.getElementById("demo").innerHTML = numbers2;
    function myFunction(value, index, array) {
        return value * 2;
    }
</script>
</body>
</html>

请留意,该函数有3个参数:

 ● 元素值

 ● 元素索引

 ● 数组本身

当回调函数仅使用value参数时,可以省略索引和数组参数:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array.map()</h2>
<p>通过对每个数组元素施行函数来创立新数组。</p>
<p id="demo"></p>
<script>
    var numbers1 = [45, 4, 9, 16, 25];
    var numbers2 = numbers1.map(myFunction);
    document.getElementById("demo").innerHTML = numbers2;
    function myFunction(value) {
        return value * 2;
    }
</script>
</body>
</html>

Array.filter()

filter()办法创立一个新数组,其中包括传递测试的数组元素。此示例从值大于18的元素创立新数组:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>JavaScript Array.filter()</title>
<body>
<h2>JavaScript Array.filter()</h2>
<p>使用通过测试的所有数组元素创立一个新数组。</p>
<p id="demo"></p>
<script>
    var numbers = [45, 4, 9, 16, 25];
    var over18 = numbers.filter(myFunction);

    document.getElementById("demo").innerHTML = over18;

    function myFunction(value, index, array) {
        return value > 18;
    }
</script>
</body>
</html>

请留意,该函数有3个参数:

 ● 元素值

 ● 元素索引

 ● 数组本身

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>JavaScript Array.filter()</title>
<body>
<h2>JavaScript Array.filter()</h2>
<p>使用通过测试的所有数组元素创立一个新数组。</p>
<p id="demo"></p>
<script>
    var numbers = [45, 4, 9, 16, 25];
    var over18 = numbers.filter(myFunction);
    document.getElementById("demo").innerHTML = over18;
    function myFunction(value) {
        return value > 18;
    }
</script>
</body>
</html>

本文来自 js教程 栏目,欢迎学习!

以上就是forEach()、Array.map()和Array.filter()如何用?(代码示例)的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有151人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板