1. 程式人生 > 其它 >javascript中的lambda表示式 find() filter() map()

javascript中的lambda表示式 find() filter() map()

javascript中的lambda表示式

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <script>
        var nubList = [
                {
                    expressId: "00001", //快遞單號ID
                    expressNum: "資料1"
                },
                {
                    expressId: 
"00002", //快遞單號ID expressNum: "資料2" }, { expressId: "00003", //快遞單號ID expressNum: "資料3" }, { expressId: "00004", //快遞單號ID expressNum: "資料4" } ];
        </span><span style="color: rgba(0, 0, 255, 1)">var</span> nub = nubList.find(nub =&gt; nub.expressId == <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">00002</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
        console.log(nub.expressId);
        
        </span><span style="color: rgba(0, 0, 255, 1)">var</span> nub1 = nubList.filter(nub =&gt; nub.expressId == <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">00002</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
        console.log(nub1[</span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">].expressId);
</span>&lt;/script&gt;

</body>
</html>

資料來自網上。

find和filter這兩個的區別是:

find返回的是一條資料;

filter返回的是一個數組;

<div id="blog_post_info">
好文要頂 關注我 收藏該文 屌絲大叔的筆記
關注 - 6
粉絲 - 38 +加關注 0
0
<div class="clear"></div>
<div id="post_next_prev">

<a href="https://www.cnblogs.com/subendong/p/9232548.html" class="p_n_p_prefix">« </a> 上一篇:    <a href="https://www.cnblogs.com/subendong/p/9232548.html" title="釋出於 2018-06-27 10:13">使用Layui和Vue實現分頁</a>
<br>
<a href="https://www.cnblogs.com/subendong/p/9272062.html" class="p_n_p_prefix">» </a> 下一篇:    <a href="https://www.cnblogs.com/subendong/p/9272062.html" title="釋出於 2018-07-06 10:05">spring @Transactional註解無效</a>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <script>
        var nubList = [
                {
                    expressId: "00001", //快遞單號ID
                    expressNum: "資料1"
                },
                {
                    expressId: "00002", //快遞單號ID
                    expressNum: "資料2"
                },
                {
                    expressId: "00003", //快遞單號ID
                    expressNum: "資料3"
                },
                {
                    expressId: "00004", //快遞單號ID
                    expressNum: "資料4"
                }
            ];
        </span><span style="color: rgba(0, 0, 255, 1)">var</span> nub = nubList.find(nub =&gt; nub.expressId == <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">00002</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
        console.log(nub.expressId);
        
        </span><span style="color: rgba(0, 0, 255, 1)">var</span> nub1 = nubList.filter(nub =&gt; nub.expressId == <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">00002</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
        console.log(nub1[</span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">].expressId);
</span>&lt;/script&gt;

</body>
</html>

資料來自網上。

find和filter這兩個的區別是:

find返回的是一條資料;

filter返回的是一個數組;

https://www.cnblogs.com/subendong/p/9246804.html