<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        es6 filter() 數組過濾方法總結

        來源:懂視網 責編:小采 時間:2020-11-27 21:59:23
        文檔

        es6 filter() 數組過濾方法總結

        es6 filter() 數組過濾方法總結:Array.every(x=>x)是每一個都要滿足 Array.some(x=>x)是有一個滿足。 Array.find(findIndex),返回符合條件的第一個值。 Array.filter(過濾成新的數組) 數組的方法分為兩類 1)改變原數組 push,pop,shift,unshift,sor
        推薦度:
        導讀es6 filter() 數組過濾方法總結:Array.every(x=>x)是每一個都要滿足 Array.some(x=>x)是有一個滿足。 Array.find(findIndex),返回符合條件的第一個值。 Array.filter(過濾成新的數組) 數組的方法分為兩類 1)改變原數組 push,pop,shift,unshift,sor

        Array.every(x=>x)是每一個都要滿足

        Array.some(x=>x)是有一個滿足。

        Array.find(findIndex),返回符合條件的第一個值。

        Array.filter(過濾成新的數組)

        數組的方法分為兩類

        1)改變原數組

        push,pop,shift,unshift,sort,reverse,splice

        2)不改變原數組concat,join-->

        split,toStringpush:從數組最后一位開始加數據

        pop:把數組最后一位剪切

        shift:在數組最前一位剪切

        unshift:在數組最前一位加數

        reverse:把原數組逆轉

        splice:arr.splice(從第幾位開始,截取多少長度,在切口處添加新數據)

        concat :連接join:返回字符串

        slice:截取arr.slice(從該為開始截取,截取到該為)

        示例

        1.創建一個數組,判斷數組中是否存在某個值

        var newarr = [
         { num: 1, val: 'ceshi', flag: 'aa' },
         { num: 2, val: 'ceshi2', flag: 'aa2' }
        ]
        console.log(newarr.filter(item => item.num===2 ))

        2.也可以通過上面方法過濾掉num為2的留下num為1的

        var newarr = [
         { num: 1, val: 'ceshi', flag: 'aa' },
         { num: 2, val: 'ceshi2', flag: 'aa2' }
        ]
        console.log(newarr.filter(item => item.num!=2 ))

        3.去掉空數組空字符串、undefined、null

        var arr = ['1','2',undefined, '3.jpg',undefined]
        var newArr = arr.filter(item => item)
        console.log(newArr)
        
        var arr = ['1','2',null, '3.jpg',null]
        var newArr = arr.filter(item => item)
        console.log(newArr)
        
        >//空字符串里面不能包含空格
        var arr = ['1','2','', '3.jpg','']
        var newArr = arr.filter(item => item)
        console.log(newArr)

        4.去掉數組中不符合項

        var arr = [20,30,50, 96,50]
        var newArr = arr.filter(item => item>40) 
        console.log(newArr)

        5.過濾不符合項

        var arr = ['10','12','23','44','42']
        var newArr = arr.filter(item => item.indexOf('2')<0) 
        console.log(newArr)

        6.數組去重

        var arr = [1, 2, 2, 3, 4, 5, 5, 6, 7, 7,8,8,0,8,6,3,4,56,2];
        var arr2 = arr.filter((x, index,self)=>self.indexOf(x)===index) 
        console.log(arr2); //[1, 2, 3, 4, 5, 6, 7, 8, 0, 56]

        7

        /*
         有一個對象數組 a ,將a數中對象某個屬性的值存儲到B數組中
        */ 
        var porducts = [
         {name:"cucumber",type:"vegetable"},
         {name:"banana",type:"fruit"},
         {name:"celery",type:"vegetable"},
         {name:"orange",type:"fruit"},
        ];
        // es5
        var filteredProducts = [];
        for(var i = 0;i < porducts.length; i ++){
         if(porducts[i].type === "fruit"){
         // 如果條件滿足就把當前的值推入
         filteredProducts.push(porducts[i])
         }
        }
        // console.log(filteredProducts)//0: {name: "banana", type: "fruit"}1: {name: "orange", type: "fruit"}length: 2__proto__: Array(0)
        // ES6
         var filter2 = porducts.filter(function(porduct){//對porducts數組對象過濾如果porduct.type === "fruit"就return出去,再用一個變量接住
         return porduct.type === "fruit"
        })
        console.log(filter2)
        

        8

        /*
         需求二
         有一個對象數組A,過濾掉不滿足以下條件對象
         條件:蔬菜 數量大于0 價格小于10
        */ 
        var products = [
         {name:"cucumber",type:"vegetable",quantity:0,price:1},
         {name:"banana",type:"fruit",quantity:10,price:16},
         {name:"celery",type:"vegetable",quantity:30,price:8},
         {name:"orange",type:"fruit",quantity:3,price:6},
        ];
        products = products.filter(function(product){
         return product.type === "vegetable"
         && product.quantity > 0
         && product.price < 10
        })
        console.log(products)//0: {name: "celery", type: "vegetable", quantity: 30, price: 8}name: "celery"price: 8quantity: 30type: "vegetable"__proto__: Objectlength: 1__proto__: Array(0)
        

        9

        /*
         需求三:
         有兩個數組A,B,根據A中的ID值 ,過濾掉B數組不符合的數據
        */ 
        var post = {id:4,title:"javascript"};
        var comments = [
         {postId:4,content:'Angular4'},
         {postId:2,content:'VUE.js'},
         {postId:3,content:'Node.js'},
         {postId:4,content:'React.js'},
        ];
        function commentsForPost(post,comments){
         return comments.filter(function(comment){
         return comment.postId === post.id;
         })
        }
        console.log(commentsForPost(post,comments))
        // 0: {postId: 4, content: "Angular4"}1: {postId: 4, content: "React.js"}length: 2__proto__: Array(0)

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        es6 filter() 數組過濾方法總結

        es6 filter() 數組過濾方法總結:Array.every(x=>x)是每一個都要滿足 Array.some(x=>x)是有一個滿足。 Array.find(findIndex),返回符合條件的第一個值。 Array.filter(過濾成新的數組) 數組的方法分為兩類 1)改變原數組 push,pop,shift,unshift,sor
        推薦度:
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 老汉精品免费AV在线播放| 精品国产一区二区三区免费| 亚洲中文字幕无码久久2017| 无码少妇一区二区浪潮免费| 两个人看www免费视频| 香蕉视频在线观看免费| 亚洲人成毛片线播放| 久久亚洲私人国产精品vA| 中文字幕中韩乱码亚洲大片| 亚洲AV成人精品日韩一区18p| 黄色片在线免费观看| 67pao强力打造国产免费| 热99RE久久精品这里都是精品免费 | 99热亚洲色精品国产88| 亚洲精品美女在线观看播放| 久久久亚洲精品国产| 亚洲人成在线影院| 亚洲毛片免费视频| 亚洲黄色激情视频| 亚洲AV无码一区二区乱子仑| 久久人午夜亚洲精品无码区| 在线观看国产一区亚洲bd| 国产精品免费看久久久香蕉| a级毛片100部免费观看| 免费国产污网站在线观看15| 国产无人区码卡二卡三卡免费| 午夜成人免费视频| 亚洲日韩国产成网在线观看| 久久亚洲精品成人| 亚洲男人天堂2018av| 国产无遮挡色视频免费观看性色| 久久免费看少妇高潮V片特黄| 成年性生交大片免费看| 国产网站在线免费观看| 亚洲精品免费观看| 免费人成大片在线观看播放| 99无码人妻一区二区三区免费| 拔擦拔擦8x华人免费久久| 99久久亚洲综合精品成人网| 日韩一区二区三区免费播放| 亚洲精品在线免费观看视频 |